如何使用Python PIL创建多帧图像 [英] How to create a multiple frame image using Python PIL
问题描述
如何使用python PIL创建新图像,其中包含多个帧。
new_Image = Image.new(I; 16,(num_pixels,num_rows))
对于范围内的帧((len(final_rows)/ num_rows)):
pixels = new_Image.load()
范围内的行(num_rows):
row_pixel = final_rows [ row] .getPixels()
范围内的像素(num_pixels):
pixels [pixel,row] = row_pixel [pixel]
print frame
new_Image.seek(frame)
我尝试使用上面的代码,但它给了我一个EOFError。代码通过将我拥有的总行数除以每帧的行数来获得帧数。然后它将数据用作像素数据。我试图寻找一个新的框架,但我想它还没有被创建。如何创建这些框架?
编辑:我想要.tif文件格式
new_Image
是您加载的内容,即读取 - 如果它没有多个帧,则不能继续下一步。
目前PIL不支持创建多帧GIF,因此您需要切换到
现在用于TiFF文件
首先我安装了tiffile, pip install -U tiffile
,我已经安装了numpy ,然后:
#coding:utf-8
来自__future__ import print_function #Python 2/3兼容性
import glob
来自PIL import Image
import tifffile
import numpy
def PIL2array(img):
将PIL / Pillow图像转换为numpy数组
返回numpy。 array(img.getdata(),
numpy.uint8).reshape(img.size [1],img.size [0],3)
FRAMES = []#空框架列表
FIRST_SIZE =无#我要说第一个文件的大小合适
OUT_NAME =test.tiff#名称保存到
f ilelist = glob.glob(*。jpg)#对于这个测试,我只是按照它们的顺序使用当前目录中的图像
#将图像放入数组
for fn in filelist :#对于列表中的每个名称
img = Image.open(fn)#如果FIRST_SIZE为None,则读取图像
:#没有大小
FIRST_SIZE = img.size#所以使用这个
如果img.size == FIRST_SIZE:#检查当前图像大小是否正常我们可以使用它
print(添加:,fn)#显示一些进展
FRAMES.append(img)#将它添加到我们的帧列表
else:
print(Discard:,fn,img.size,<>,FIRST_SIZE)#你可以调整大小并附加在这里!
print(Writing,len(FRAMES),frames to,OUT_NAME)
,tifffile.TiffWriter(OUT_NAME)为tiff:
for img in FRAMES:
tiff.save(PIL2array(img),compress = 6)
print(Done)
哪个产生了一个很好的tiff文件,但不幸的是SO浏览器没有显示多页tiff文件,至少认为这些文件是可以接受的,如下所示。
< a href =https://i.stack.imgur.com/Gsy5Q.png =nofollow noreferrer>
How would I go about creating a new image, using python PIL, which has multiple frames in it.
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)
I tried using the above code but it gives me a EOFError. The code gets the number of frames by dividing the total number of rows that I have by the number of rows per frame. It then uses the data as pixel data. I was trying to seek a new frame but I guess it hasn't been created. How do I create these frames?
Edit: I would like a .tif file format
new_Image
is what you have loaded, i.e. read - if it doesn't have more than one frame you can not move on the the next.
For the moment PIL does not have support for creating Multi-Frame GIFs so you would need to switch to pillow which does - there is some sample code on GitHub.
Update
Took the time to investigate further and found a bug fixed version of images2gif
so what I did was to use pip install images2gif
and then downloaded the bug fixed version from here and overwrote the one installed by pip
but you could just download that bug fix version and put it in the same directory as your development file.
I then created a CreateGif.py file:
# 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")
Running this in my test directory resulted in:
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
And produced:
And Now For TiFF files
First I installed tiffile, pip install -U tiffile
, I already had numpy installed, then:
# 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")
Which produced a nice tiff file, but unfortunately the SO viewer does not display multi-page tiff files that windows at least thinks is acceptable as can be seen below.
这篇关于如何使用Python PIL创建多帧图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!