尝试从一组图像创建 GIF

use*_*895 5 python

.gif我正在尝试使用 python从一组 PIL 图像创建动画。

这是我到目前为止所拥有的:

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()
Run Code Online (Sandbox Code Playgroud)

这是错误:

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
Run Code Online (Sandbox Code Playgroud)

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

Tim*_*Tim 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()
Run Code Online (Sandbox Code Playgroud)

生成的 GIF

  • 新错误:“TypeError:必须是字符串或缓冲区,而不是 None” (3认同)