如何使用Python PIL创建多帧.tif图像

如何使用Python PIL创建多帧.tif图像,python,image,python-imaging-library,tiff,Python,Image,Python Imaging Library,Tiff,我将如何使用pythonpil创建一个新的图像,其中包含多个帧 new_Image = Image.new("I;16", (num_pixels,num_rows)) for frame in range((len(final_rows)/num_rows)): pixels = new_Image.load() for row in range(num_rows): row_pixel = final_rows[row].getPixels()

我将如何使用pythonpil创建一个新的图像,其中包含多个帧

new_Image = Image.new("I;16", (num_pixels,num_rows))

for frame in range((len(final_rows)/num_rows)):
    pixels = new_Image.load()

    for row in range(num_rows):
        row_pixel = final_rows[row].getPixels()
        for pixel in range(num_pixels):
            pixels[pixel,row] = row_pixel[pixel]
    print frame
    new_Image.seek(frame)
我尝试使用上面的代码,但它给了我一个EOR。代码通过将我拥有的总行数除以每帧的行数来获得帧数。然后将数据用作像素数据。我试图寻找一个新的框架,但我想它还没有被创造出来。如何创建这些框架


编辑:我想要一个.tif文件格式

新图像
是您已加载的图像,即读取-如果它没有多个帧,则无法移动到下一帧

目前,PIL还不支持创建多帧GIF,因此您需要切换到哪个GIF—上面有一些示例代码

更新 花了时间进行进一步调查,发现了
images2gif
的bug修复版本,因此I所做的是使用
pip安装images2gif
,然后从
pip
下载bug修复版本并将其改写,但您可以下载该bug修复版本并将其放在同一版本中目录作为您的开发文件

然后我创建了一个CreateGif.py文件:

# coding: utf-8
from __future__ import print_function # Python 2/3 compatibility
import glob
from PIL import Image
from images2gif import writeGif

DELAY=0.75  # How long between frames
FRAMES = [] # Empty list of frames
FIRST_SIZE = None # I am going to say that the first file is the right size
OUT_NAME = "test.gif" # Name to save to
filelist = glob.glob("*.jpg") # For this test I am just using the images in the current directory in the order they are

for fn in filelist:  # For each name in the list
    img = Image.open(fn) # Read the image
    if FIRST_SIZE is None:  # Don't have a size
        FIRST_SIZE = img.size  # So use this one
    if img.size == FIRST_SIZE: # Check the current image size if it is OK we can use it
        print ("Adding:", fn)  # Show some progress
        FRAMES.append(img)   # Add it to our frames list
    else:
        print ("Discard:", fn, img.size, "<>", FIRST_SIZE) # You could resize and append here!

print("Writing", len(FRAMES), "frames to", OUT_NAME)
writeGif(OUT_NAME, FRAMES, duration=DELAY, dither=0)
print("Done")
这产生了一个很好的tiff文件,但不幸的是,SO查看器没有显示windows至少认为可以接受的多页tiff文件,如下所示。

从第一帧的PIL图像开始,然后保存该图像,并将列表中的其他帧图像传递到
append_images
参数,然后全部保存。它适用于
webp
gif
tiff
格式,尽管
tiff
似乎忽略了持续时间和循环参数:

im1.save(save_to, format="tiff", append_images=[im2], save_all=True, duration=500, loop=0)

另请参见。

新图像是我使用
new\u image=image.new(“I;16”,“num\u像素,num\u行”)创建的。
。该示例代码似乎没有创建新图像,只访问现有图像
im=image.open(infle)
。您需要提供一组图像作为示例的输入。顺便说一句,Image.new不是您示例代码中的内容。很抱歉,它出现在上面的行中,因为gifmaker已合并到枕头中,第二个URL不再相关:p(我仍在努力创建一个多帧图像)@RomualdBrunet-添加了一些gif和tiff的工作示例。您尝试过这个吗:@Paul不,我没有尝试过。我真的不想创建电影或Gif。我想要的文件格式是.tif。
# coding: utf-8
from __future__ import print_function # Python 2/3 compatibility
import glob
from PIL import Image
import tifffile
import numpy

def PIL2array(img):
    """ Convert a PIL/Pillow image to a numpy array """
    return numpy.array(img.getdata(),
        numpy.uint8).reshape(img.size[1], img.size[0], 3)
FRAMES = [] # Empty list of frames
FIRST_SIZE = None # I am going to say that the first file is the right size
OUT_NAME = "test.tiff" # Name to save to
filelist = glob.glob("*.jpg") # For this test I am just using the images in the current directory in the order they are
# Get the images into an array
for fn in filelist:  # For each name in the list
    img = Image.open(fn) # Read the image
    if FIRST_SIZE is None:  # Don't have a size
        FIRST_SIZE = img.size  # So use this one
    if img.size == FIRST_SIZE: # Check the current image size if it is OK we can use it
        print ("Adding:", fn)  # Show some progress
        FRAMES.append(img)   # Add it to our frames list
    else:
        print ("Discard:", fn, img.size, "<>", FIRST_SIZE) # You could resize and append here!

print("Writing", len(FRAMES), "frames to", OUT_NAME)
with tifffile.TiffWriter(OUT_NAME) as tiff:
    for img in FRAMES:
        tiff.save(PIL2array(img),  compress=6)
print("Done")
im1.save(save_to, format="tiff", append_images=[im2], save_all=True, duration=500, loop=0)