我正在尝试使用python从一组PIL图像中创建动画.gif
。
这是我到目前为止所做的:
from images2gif import writeGif
from PIL import Image, ImageDraw
import os
import sys
import random
import argparse
import webbrowser
filename = ""
def makeimages():
for z in range(1, 31):
dims = (400, 400) # size of image
img = Image.new('RGB', dims) # crete new image
draw = ImageDraw.Draw(img)
r = int(min(*dims)/100)
print "Image img%d.png has been created" % z
n = 1000
for i in range(n):
x, y = random.randint(0, dims[0]-r), random.randint(0, dims[1]-r)
fill = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
draw.ellipse((x-r, y-r, x+r, y+r), fill)
img.save('.img%d.png' % z)
def makeAnimatedGif():
# Recursively list image files and store them in a variable
path = "./Images/"
os.chdir(path)
imgFiles = sorted((fn for fn in os.listdir('.') if fn.endswith('.png')))
# Grab the images and open them all for editing
images = [Image.open(fn) for fn in imgFiles]
global filename
filename = filename + ".gif"
writeGif(filename, images, duration=0.2)
print os.path.realpath(filename)
print "%s has been created, I will now attempt to open your" % filename
print "default web browser to show the finished animated gif."
#webbrowser.open('file://' + os.path.realpath(filename))
def start():
print "This program will create an animated gif image from the 30 images provided."
print "Please enter the name for the animated gif that will be created."
global filename
filename = raw_input("Do Not Use File Extension >> ")
print "Please wait while I create the images......"
makeimages()
print "Creating animated gif...."
makeAnimatedGif()
start()
以下是错误:
Traceback (most recent call last):
File "Final.py", line 60, in <module>
start()
File "Final.py", line 56, in start
makeimages()
File "Final.py", line 30, in makeimages
img.save('Images/.img%d.png' % z)
File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 1439, in save
save_handler(self, fp, filename)
File "/usr/local/lib/python2.7/dist-packages/PIL/PngImagePlugin.py", line 572, in _save
ImageFile._save(im, _idat(fp, chunk), [("zip", (0,0)+im.size, 0, rawmode)])
File "/usr/local/lib/python2.7/dist-packages/PIL/ImageFile.py", line 481, in _save
e = Image._getencoder(im.mode, e, a, im.encoderconfig)
File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 401, in _getencoder
raise IOError("encoder %s not available" % encoder_name)
IOError: encoder zip not available
所需的输出是让python创建30个图像,然后将它们组合在一起并将其保存为GIF文件。
答案 0 :(得分:2)
您的代码中有一个拼写错误。应该有意img.save('.img%d.png' % z)
。
您的代码中的主要错误是生成的图片不在./Images/
中,您从中生成gif。
你的目录中不存在./Images/
./Images/
。
下面的代码是一个修复程序,它可以运行。
from images2gif import writeGif
from PIL import Image, ImageDraw
import os
import sys
import random
import argparse
import webbrowser
filename = ""
def makeimages():
# Create the dir for generated images
if not os.path.exists("Images"):
os.makedirs("Images")
for z in range(1, 31):
dims = (400, 400) # size of image
img = Image.new('RGB', dims) # crete new image
draw = ImageDraw.Draw(img)
r = int(min(*dims)/100)
print "Image img%d.png has been created" % z
n = 1000
for i in range(n):
x, y = random.randint(0, dims[0]-r), random.randint(0, dims[1]-r)
fill = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
draw.ellipse((x-r, y-r, x+r, y+r), fill)
img.save('Images/.img%d.png' % z)
def makeAnimatedGif():
# Recursively list image files and store them in a variable
path = "./Images/"
os.chdir(path)
imgFiles = sorted((fn for fn in os.listdir('.') if fn.endswith('.png')))
# Grab the images and open them all for editing
images = [Image.open(fn) for fn in imgFiles]
global filename
filename = filename + ".gif"
writeGif(filename, images, duration=0.2)
print os.path.realpath(filename)
print "%s has been created, I will now attempt to open your" % filename
print "default web browser to show the finished animated gif."
#webbrowser.open('file://' + os.path.realpath(filename))
def start():
print "This program will create an animated gif image from the 30 images provided."
print "Please enter the name for the animated gif that will be created."
global filename
filename = raw_input("Do Not Use File Extension >> ")
print "Please wait while I create the images......"
makeimages()
print "Creating animated gif...."
makeAnimatedGif()
start()