OddThinking

A blog for odd things and odd thoughts.

Python Imaging Library (PIL) and Animated GIFs

Summary

Let me save the next person some trouble: The Python Imaging Library (PIL 1.1.5) does not support writing animated GIFs.

Update: A script that has been published may help. See comments.

Background

It was suggested to me that my previous post needed a few diagrams to explain the concepts. I thought about it the number of dimensions I was trying to explain, and decided that some simple machine-generated animations were probably the way to go here.

When I hear machine-generated images, I think of the Python Imaging Library. It has proven to be a very powerful tool on a number of small-to-medium toy projects that I have attempted.

I imagined that I would write a hundred or so lines of Python code that would spit out images of intersections in various states of traffic flow, and I would combine them together in a sequence to produce a a few animated GIFs, and then link the GIFs into appropriate places in the article.

(Some of you are cringing at the idea of getting involved in the GIF ex-controversy, but I wanted animation, and couldn’t rely on browser support for the uncontroversial GIF-alternative, MNG.)

Findings

There are two GIF versions, GIF87a and GIF89a. For reasons I don’t quite get, even though GIF87a supports having multiple images, but GIF89a is required for real animation.

PIL 1.1.5 can read GIF87a and GIF89a [Ref]. It can read each of the images in the sequence (see Image.seek()).

The GIF formats also include some metadata, which appears to be only partially supported by PIL 1.1.5. I found that the Image.info() method included “duration” and “loop” fields. The May 6, 2005 draft of the next PIL handbook describes the “duration” field only.

However, PIL 1.1.5 can only write to GIF87a. It cannot write to GIF89a. Therefore, it can’t be used to produce GIF89a animations.

I do not believe it can even be used to create new GIF87a sequences. (I can’t find any suitable methods.) I believe it can be used to edit existing (but useless?) GIF87a sequences, but I haven’t tested this.

Other References

In 1997, in an online thread titled: “Can PIL handle animated GIFs?” appeared to suggest yes. I couldn’t find the referenced example code, but it seems to be a limited “yes”, and consistent with the restrictions above.

In May 2005, Ben Last complained:

The only thing that let me down was… the damn PIL. An imaging library that has some of the worst GIF support I’ve yet seen. Yes, I know all about the GIF patent issues, but de-emphasising support for a de-facto standard because of ideological convictions doesn’t work in the real world.

He suggests some alternatives. (BTW, Ben, knolp!)

Update: A script, described in the comments below, may help people looking to work-around this missing feature.


Comments

  1. IIRC, GIF87a has no concept of transitions, so you cannot define how quickly the next frame is to follow the current one, nor can you define in which way the next frame is to be drawn onto the current one. It only supports “multiple images in a file” – literally. You can store multiple independent images in a single image file, which have no relation to each other. Think of a photo album.

    The concept might even have been pretty cool, had webbrowsers offered a way to address a particular picture inside a GIF file – stick all the pictures you use throughout the site into a single file and the browser gets them all at once. That would have significantly reduced the TCP connection storm caused by HTTP in its 1.0 days.

  2. Aristotle,

    That makes sense. The GIF87a standard does seem to hint at the idea that the GIF could be animated, but I assume that wasn’t enough details for a meaningful animation, and hence wasn’t implemented by viewers.

  3. yeah, it sucks, now i have to use imagemagick from pythons shell interface os.system to convert animations to animated gifs. I did get GIF87a output to work from PIL, but without control over delay or looping, whats the point?

  4. Bundagan,

    It’s good to hear that there is another solution – ImageMagick.

    I looked briefly at that package some months ago (for a different purpose), and I recall I had some challenges installing it, but I don’t remember the details.

    Please note that there is a Python interface available from their web-site, so you should be able to avoid the clumsy os.system interface.

  5. Here is an example of how to make an animated gif using PIL:
    http://svn.effbot.python-hosting.com/pil/Scripts/gifmaker.py

  6. Jabapyth,

    Thanks very much for the link! I was very surprised to see it, so I checked it out. This is what I found.

    It is a Python script that uses the Python Imaging Library and some knowledge of the format of animated GIFs, to allow animated GIFs to be written out from a sequence of images.

    (I haven’t actually run the code, just read it, so take this with a grain of salt.)

    As far as I can make out, this script is included with the source code of PIL but is not installed with the binaries. You can’t rely on people having it installed.

    I would say that this is not an integrated part of the PIL library, but it looks like an excellent resource for someone who needs to solve this problem.

    Thanks again!

  7. I had an occasion to try the PIL script that Jabapyth mentioned and which I declared (after a caveat that I hadn’t actually tried the code) to be an excellent resource.

    I wanted to create an animated GIF. I read through the script again, looking for a way to set the frame-rate, and found there wasn’t one.

    I realised that this script doesn’t create GIF89a animated GIFs. I think it only creates GIF87a image sequences.

    So, I want to go back to my original findings, and withdraw the claim that this script offers a potential work-around.

    I believe it is still true that “PIL 1.1.5 can only write to GIF87a. It cannot write to GIF89a.”

    I originally said that I don’t believe that PIL 1.1.5 “can even be used to create new GIF87a sequences.” While that is still strictly true, the script mentioned by Jabapyth offers a work-around for the creation of (largely useless!) GIF87a image sequences. It builds the support on top of PIL.

    (By the way, PIL 1.1.6 is now available, but I don’t see any signs of GIF89a support improvements in the release notes.)

  8. but gifmaker.py does not set the delay, so the created gif will show all frames without delay.

  9. Hi,

    I wanted to make a gif movie given a list of images and like
    the way you tried to figure it out. I dug a little deeper and
    (with the help of the file format description on wikipedia)
    managed to make it possible to produce 89a animated gif files.
    I put the result on http://sites.google.com/site/almarklein/files-1/images2gif.py.

    Still, I was not happy with the result: only 256 colours and
    the dithering doesn’t make it pretty either… So I also looked
    into alternatives and ended up checking out the swf file format,
    which is very well described in a pdf on adobe’s website. After
    a days work or so I can now produce swf files too. The script
    for that can be found in the same place:
    http://sites.google.com/site/almarklein/files-1/images2Swf.py

    Hope this helpes,
    Almar

  10. There are recipes out there for creating gif animations from files in a directory (some of the above links point to them, thanks). However I want to create a gif animation from a list of images in python, for instance with PIL. What I mean is suppose I have a list
    list_of_images[1:N]
    I want to create a gif animation without needing to write all the images to files first.

    Much appreciate any help.

    Thanks, Ariel

  11. I just realised that Almar’s link does just what I’m looking for. I’m going to try it out.

    Thanks, Ariel

  12. Thank you, Almar, I learned a great deal reading your code. It completely solved a problem I was having, as well as teaching me more Python.

  13. Hi,

    it’s been a while, but I’ve updated the code quite a bit (and had some good help from others); the produced GIF files now look much better, because their palette is created adaptively.

    images2gif is now packed in a package that supports read/writing GIF, SWF, image-series, and AVI/MPG. It’s a subpackage of visvis (a visualization toolkit), but can be used independently from it. Licensed under the BSD license.

    http://code.google.com/p/visvis/source/browse/#hg/vvmovie

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <br> <code> <del datetime=""> <dd> <dl> <dt> <em> <i> <ins datetime="" cite=""> <li> <ol> <p> <q cite=""> <strike> <strong> <sub> <sup> <u> <ul>

Web Mentions

  1. Websites tagged "pil" on Postsaver

  2. Automated Animated GIF Adventure | divsoup

  3. Crear un archivo GIF animado con Gimp | El atareao

  4. Programmatically generate video or animated GIF in Python? - Tech Forum Network

  5. Programmatically generate video or animated GIF in Python? | Ask & Answers

  6. Programmatically generate video or animated GIF in Python? | Yotter Answers