Python 尝试从一组图像创建GIF

Python 尝试从一组图像创建GIF,python,Python,我正在尝试使用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): d

我正在尝试使用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
回溯(最近一次呼叫最后一次):
文件“Final.py”,第60行,在
开始()
文件“Final.py”,第56行,开头
makeimages()
makeimages中第30行的文件“Final.py”
img.save('Images/.img%d.png“%z”)
文件“/usr/local/lib/python2.7/dist-packages/PIL/Image.py”,第1439行,保存
保存\u处理程序(self、fp、filename)
文件“/usr/local/lib/python2.7/dist packages/PIL/PngImagePlugin.py”,第572行,保存
图像文件。_save(im,_idat(fp,chunk),[((zip),(0,0)+im.size,0,rawmode)])
文件“/usr/local/lib/python2.7/dist packages/PIL/ImageFile.py”,第481行,保存
e=图像。_getencoder(im.mode,e,a,im.encoderconfig)
文件“/usr/local/lib/python2.7/dist packages/PIL/Image.py”,第401行,在_getencoder中
raise IOError(“编码器%s不可用”%encoder\u name)
IOError:编码器zip不可用

所需的输出是让python创建30个图像,然后将它们组合在一起并保存为GIF文件。

代码中有一个输入错误<代码>img.save('.img%d.png'%z)应该是预期的

代码中的主要缺陷是生成的图像不在生成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()

请编辑您的代码以包含错误和回溯。谢谢我可以说通过全局函数传递参数是一种真正的代码味道吗?请不要那样做。哦,天哪。那是我的错,我会加上它们。抱歉:/@Mark Ransom,那么什么是更合适的方法。将信息作为实际参数传递到函数中。尝试了上述错误,但仍然存在相同的错误。在我的mac中可以。你的错误是什么?如果仍然
IOError:encoder zip不可用
您可以检查以解决它。我上面的代码与stackoverflow post具有相同的推荐导入。并且它似乎仍然不起作用。以下是错误:我将错误添加到OP中。新错误:
TypeError:必须是字符串或缓冲区,而不是无