views:

3149

answers:

7

I have a series of images that I want to create a video from. Ideally I could specify a frame duration for each frame but a fixed frame rate would be fine too. I'm doing this in wxPython, so I can render to a wxDC or I can save the images to files, like PNG. Is there a Python library that will allow me to create either a video (AVI, MPG, etc) or an animated GIF from these frames?

Edit: I've already tried PIL and it doesn't seem to work. Can someone correct me with this conclusion or suggest another toolkit? This link seems to backup my conclusion regarding PIL: http://www.somethinkodd.com/oddthinking/2005/12/06/python-imaging-library-pil-and-animated-gifs/

A: 

The python imaging library has an example that will do this for you:

http://www.pythonware.com/products/pil/

The license is similar to a BSD license (free to use, must include copyright and license in supporting documentation).

Adam Davis
+6  A: 

Well, now I'm using ImageMagick. I save my frames as PNG files and then invoke ImageMagick's convert.exe from Python to create an animated GIF. The nice thing about this approach is I can specify a frame duration for each frame individually. Unfortunately this depends on ImageMagick being installed on the machine. They have a Python wrapper but it looks pretty crappy and unsupported. Still open to other suggestions.

FogleBird
+2  A: 

It's not a python library, but mencoder can do that: Encoding from multiple input image files. You can execute mencoder from python like this:

import os

os.system("mencoder ...")
wbowers
+2  A: 

Have you tried PyMedia? I am not 100% sure but it looks like this tutorial example targets your problem.

Jakub Šturc
A: 

I'm finding conflicting info about whether or not PIL will support creation of animated gifs.

Also, is PIL compatible with version 2.6?

epohs
No, it doesn't support animated GIFs and yes, it does work with Python 2.6.
exclipy
+3  A: 

To create a video, you could use opencv,

#load your frames
frames = ...
#create a video writer
writer = cvCreateVideoWriter(filename, -1, fps, frame_size, is_color=1)
#and write your frames in a loop if you want
cvWriteFrame(writer, frames[i])
attwad
A: 

As of June 2009 the originally cited blog post has a method to create animated GIFs in the comments. Download the script images2gif.py.

Then, to reverse the frames in a gif, for instance:

#!/usr/bin/env python

from PIL import Image, ImageSequence
import sys, os
filename = sys.argv[1]
im = Image.open(filename)
original_duration = im.info['duration']
frames = [frame.copy() for frame in ImageSequence.Iterator(im)]    
frames.reverse()

from images2gif import writeGif
writeGif("reverse_" + os.path.basename(filename), frames, duration=original_duration/1000.0, dither=0)
kostmo