5

I'm trying to use python to create an animated .gif from a set of PIL images.

Here is what I have so far:

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()

Here is the errors:

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

The desired output for this is to have python create 30 images, then combine them all together and save it out as a GIF file.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
user181895
  • 317
  • 1
  • 4
  • 9

1 Answers1

2

There are one typo in your code. img.save('.img%d.png' % z) should be intended.

And the main bug in your code is the generated images is not in ./Images/ you generate gif from.

And you should make ./Images/ is ./Images/ is not exist in your dir.

Code below is a fix, and it works.

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()

Generated Gif

Tim
  • 2,121
  • 2
  • 20
  • 30
  • Tried the above and same error. – user181895 Dec 07 '15 at 06:08
  • It's OK in my mac. What's your error? If still `IOError: encoder zip not available` you may check http://stackoverflow.com/questions/3544155/about-the-pil-error-ioerror-decoder-zip-not-available to solve it. – Tim Dec 07 '15 at 06:48
  • My code above has the same recommanded imports as the stackoverflow post.and it still does not seem to work here are the errors: – user181895 Dec 07 '15 at 07:24
  • I added the error to the OP. – user181895 Dec 07 '15 at 07:27
  • 3
    New error: `TypeError: must be string or buffer, not None` – user181895 Dec 07 '15 at 07:41
  • @user181895 You should really Google this error, see post in http://stackoverflow.com/questions/19149643/error-in-images2gif-py-with-globalpalette. I just `sudo pip install images2gif` for the `images2gif` library and it seems all ok. – Tim Dec 07 '15 at 08:22
  • It's been installed since the stat of this post. – user181895 Dec 07 '15 at 08:57
  • What I mean is the code here is no problem, and your error seems the library you used is not ok. So you should check your library rather than the code. – Tim Dec 07 '15 at 09:00
  • my apologies. i misunderstood. i will try a different library. – user181895 Dec 07 '15 at 09:22
  • I found a part of my code that seems to be the problem. In the `makeanamatedGif` function the way I have defined `filename = filename + ".gif"` is the problem. When it goes to write the GIF, it sees the filename parameter as blank. That is what gives the `TypeError: must be string or buffer, not None`. I have been doing this all on Ubuntu 15.04 so I will try a second platform to be sure. – user181895 Dec 07 '15 at 15:43
  • Issues has been resolved thanks to @Tim. Thank you to everyone that commented for your assistance. Turns out it was a libary issue and I downladed the new one from github [link]https://github.com/rec/echomesh/blob/master/code/python/external/images2gif.py – user181895 Dec 07 '15 at 15:49