Skip to content Skip to sidebar Skip to footer

Gifs Opened With Python Have Broken Frames

Sometimes when opening gifs and saving separate frames to files, the frames come out in bad shape. This doesn't happen with all the gifs, but with the ones that does it happens to

Solution 1:

In gifs a frame may contain only the pixels that changed in that frame. So when you export you get black where there was no change.

from PIL import Image

img = Image.open('pigs.gif')

counter = 0
collection = []
current = img.convert('RGBA')
whileTrue:
    try:
        current.save('original%d.png' % counter)
        img.seek(img.tell()+1)
        current = Image.alpha_composite(current, img.convert('RGBA'))
        counter += 1except EOFError:
        break

EDIT: Changed output format to png as suggested in comments due to color palette problems that otherwise occur.

Post a Comment for "Gifs Opened With Python Have Broken Frames"