Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GIF split into frames saves corrupted png images #4810

Closed
noambaron1989 opened this issue Jul 23, 2020 · 3 comments
Closed

GIF split into frames saves corrupted png images #4810

noambaron1989 opened this issue Jul 23, 2020 · 3 comments
Labels

Comments

@noambaron1989
Copy link

noambaron1989 commented Jul 23, 2020

I'm trying to split gif into frames and save as PNG.
The issue is, some gifs are low quality and when getting to the second frame onwards, the frames are corrupted.

this is the code I'm using:

import logging
import os

from PIL import Image
from logger import Logger

toddler_lq = '/Users/noambaron/Downloads/toddler_LQ.gif'
toddler_dir = '/Users/noambaron/Desktop/toddler_f'
gif_path = toddler_lq
frame_dir = toddler_dir
logger = Logger(logging.INFO, 'gif_test')

try:
    frame = Image.open(gif_path)
    palette = frame.palette
    num_of_frames = 0
    while frame:
        base_name = os.path.basename(gif_path).split('.')[0]
        frame.show()
        frame.save(os.path.join(frame_dir, '%s-%s.png' % (base_name, num_of_frames)), 'PNG')
        num_of_frames += 1
        try:
            frame.seek(num_of_frames)
        except EOFError:
            break
except Exception as e:
    logger.error('failed extracting frames from gif %s due to %s', gif_path, e)

attached is the gif, and the 3 first frames
toddler_LQ-2
toddler_LQ-1
toddler_LQ-0
toddler_LQ

@hugovk hugovk added the GIF label Jul 23, 2020
@radarhere radarhere changed the title GIF split into frames save corrupted png images GIF split into frames saves corrupted png images Dec 24, 2020
@peterableda
Copy link

peterableda commented May 23, 2021

I have the same issue with similar code:

import PIL
from PIL import Image
from PIL import ImageOps
from PIL import GifImagePlugin

gif = Image.open('/home/pi/Photos/ANIMATION.gif')

# for frame in ImageSequence.Iterator(gif):
for n in range(0,gif.n_frames):
    gif.seek(n)
    im = ImageOps.grayscale(gif)
    im.save("/home/pi/Photos/tmp/gif_{}.bmp".format(n))

@radarhere
Copy link
Member

@peterableda please open a new issue with your specific image.

@radarhere
Copy link
Member

Pillow 9.0.0 has significantly improved reading animated GIFs thanks to #5857.

If this issue is still a problem when using Pillow 9.0.0, please let us know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants