4

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

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)

我尝试使用上面的代码,但它给了我一个 EOFError。该代码通过将我拥有的总行数除以每帧的行数来获取帧数。然后它将数据用作像素数据。我试图寻找一个新的框架,但我猜它还没有被创建。如何创建这些框架?

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

4

2 回答 2

1

new_Image是您已加载的内容,即已阅读的内容-如果它没有超过一帧,则您无法继续下一帧。

目前 PIL 不支持创建多帧 GIF,因此您需要切换到支持的枕头- GitHub 上有一些示例代码。

更新

花时间进一步调查并发现了一个错误修复版本,images2gif所以所做的是使用pip install images2gif然后从这里下载错误修复版本并覆盖了安装的版本,pip但您可以下载该错误修复版本并将其放在同一个目录作为您的开发文件。

然后我创建了一个 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")

在我的测试目录中运行它会导致:

F:\Uploads>python CreateGif.py
Adding: Hidden.jpg
Adding: NewJar.jpg
Adding: P1000063.JPG
Adding: P1000065.JPG
Adding: P1000089.JPG
Discard: WiFi_Virgin.jpg (370, 370) <> (800, 600)
Writing 5 frames to test.gif
Done

并制作:在此处输入图像描述

现在对于 TiFF 文件

首先我安装了 tiffile, pip install -U tiffile我已经安装了 numpy,然后:

# 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")

这产生了一个很好的 tiff 文件,但不幸的是,SO 查看器没有显示 Windows 至少认为可以接受的多页 tiff 文件,如下所示。 在此处输入图像描述

于 2013-10-23T12:02:19.970 回答
1

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

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

另请参阅文档

于 2018-09-26T10:41:33.820 回答