[Image-SIG] PIL + animated gifs + example + problems

Bram - Smartelectronix bram at smartelectronix.com
Fri Jun 1 19:24:42 CEST 2007

Hello Everyone, this is my first post here...

After looking around on the web and not finding any conclusive examples 
on how to *process* animated GIF's with PIL, I decided to write a small 
example (of course based on gifmaker.py). This script takes an input 
file, explodes it, resizes every frame and them implodes it again into 
an animated GIF. The script is included at the bottom of this email.

However, there's some problems:

1. the file written in a LOT larger than the original file (when no 
resize is being done). From a 1MB animated GIF I get a 3MB one. I tried 
the gifmaker trick of writing only the changed pixels, but this 
completely warped the image.

2. the animation speed of the end-result isn't controlable (as far as I 
know). Is there a hack/workaround for this problem?

thanks a lot,

  - bram

# here's the example script

import Image
from GifImagePlugin import getheader, getdata

# my two local examples...
input_filename = "FlrSer.gif"
output_filename = "out.gif"

im = Image.open(input_filename)

# get all the frames into a big array
frame = 0
frames = []

while 1:
         frame += 1
     except EOFError:

outfile = file(output_filename, "wb")

for (index, image) in enumerate(frames):
     # process image here...
     # this is just an example
     new_size = image.size[0]/2, image.size[1]/2
     image = image.resize( new_size )

     # only write the header at the start
     if index == 0:
         for s in getheader(image):

     for s in getdata(image):


# sadly enough we are now left with
# 1. an image that is larger than the original (??)
# 2. frames that run at a different speed than the original

More information about the Image-SIG mailing list