16

I'm very new to python, trying to use it to split the frames of a GIF into PNG images.

# Using this GIF: http://www.videogamesprites.net/FinalFantasy1/Party/Before/Fighter-Front.gif

from PIL import Image

im = Image.open('Fighter-Front.gif')
transparency = im.info['transparency'] 
im.save('test1.png', transparency=transparency)

im.seek(im.tell()+1)
transparency = im.info['transparency'] 
im.save('test2.png', transparency=transparency)

# First frame comes out perfect, second frame (test2.png) comes out black,
# but in the "right shape", i.e. 
# http://i.stack.imgur.com/5GvzC.png

Is this specific to the image I'm working with or am I doing something wrong?

Thanks!

Joseph
  • 11,738
  • 16
  • 71
  • 113

2 Answers2

18

I don't think you're doing anything wrong. See a similar issue here: animated GIF problem. It appears as if the palette information isn't correctly treated for later frames. The following works for me:

def iter_frames(im):
    try:
        i= 0
        while 1:
            im.seek(i)
            imframe = im.copy()
            if i == 0: 
                palette = imframe.getpalette()
            else:
                imframe.putpalette(palette)
            yield imframe
            i += 1
    except EOFError:
        pass

for i, frame in enumerate(iter_frames(im)):
    frame.save('test%d.png' % i,**frame.info)
DSM
  • 319,184
  • 61
  • 566
  • 472
6

I've fixed this bug here https://code.launchpad.net/~asdfghjkl-deactivatedaccount1/python-imaging/gif-fix.

DSM's answer won't work if the GIF uses local color tables.

Mu Mind
  • 10,460
  • 4
  • 34
  • 67
Peter Le Bek
  • 982
  • 8
  • 16
  • 1
    Indeed, replacing the palette like is being done in the accepted answer is a very wrong thing to do. But, without needing to apply a patch, is it only a matter whether checking `frame.palette.dirty` to decide between putting the "initial" palette ? – mmgp Jan 27 '13 at 22:43
  • Can you submit a pull request to Pillow for this? https://github.com/python-imaging/Pillow/issues/312#issuecomment-21852418 – aclark Jul 31 '13 at 10:13