Python: Converting GIF frames to PNG
Asked Answered
S

4

17

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. 
# https://i.sstatic.net/5GvzC.png

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

Thanks!

Sucker answered 5/2, 2011 at 3:25 Comment(0)
S
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)
Sherard answered 5/2, 2011 at 4:51 Comment(0)
S
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.

Saxen answered 29/4, 2011 at 20:14 Comment(2)
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 ?Bipartisan
Can you submit a pull request to Pillow for this? github.com/python-imaging/Pillow/issues/…Slicer
S
1

I believe you could remove the transparency and .info part altogether and replace it with: im.convert("RGBA") just before .save with each frame. As long as you're going to export the frames in a format that supports Alpha channel, this should achieve the desired effect.

from PIL import Image

im = Image.open('Fighter-Front.gif')
im.convert("RGBA")
im.save('test1.png')

im.seek(im.tell()+1)
im.convert("RGBA")
im.save('test2.png')
Stylize answered 8/5, 2023 at 8:24 Comment(0)
K
0

Well, DSM's code did help but got me a few errors, after which I modified it a bit, and it is now working perfectly fine for me.

Error:

Traceback (most recent call last):
  File "E:\practice codes\convertGIfToPNG\convertGIFtoPNG.py", line 20, in <module>
    for i, frame in enumerate(iter_frames(im)):
  File "E:\practice codes\convertGIfToPNG\convertGIFtoPNG.py", line 12, in iter_frames
    imframe.putpalette(palette)
  File "C:\Users\*****\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\PIL\Image.py", line 1939, in putpalette
    raise ValueError(msg)
ValueError: illegal image mode

Redefined CODE:

from PIL import Image

def iter_frames(im):
    try:
        while True:
            current_frame = im.copy()
            yield current_frame
            im.seek(im.tell() + 1)
    except EOFError:
        pass

im = Image.open(r'E:\practice codes\convertGIfToPNG\Fighter-Front.gif')

frames = []
try:
    while True:
        frames.append(im.copy())
        im.seek(im.tell() + 1)
except EOFError:
    pass

for i, frame in enumerate(frames):
    frame.save(r'E:\practice codes\convertGIfToPNG\Fighter\Fighter%d.png' % i) #Fighter here is the folder followed by the sequence of Fighter<i>.png stored inside it
Kayekayla answered 4/12, 2023 at 16:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.