PIL – Convert GIF Frames to JPG

http://stackoverflow.com/questions/10269099/pil-convert-gif-frames-to-jpg

import Image
import sys

def processImage(infile):
    try:
        im = Image.open(infile)
    except IOError:
        print "Cant load", infile
        sys.exit(1)
    i = 0
    mypalette = im.getpalette()

    try:
        while 1:
            im.putpalette(mypalette)
            new_im = Image.new("RGBA", im.size)
            new_im.paste(im)
            new_im.save('foo'+str(i)+'.png')
            #new_im.save('foo'+str(i)+'.png',quality=30, optimize=True, progressive=True)

            i += 1
            im.seek(im.tell() + 1)

    except EOFError:
        pass # end of sequence

processImage('gif_example.gif')