Pillow: How to open gif file in python 3, anybody knows a perfect method, please do a favour on me

Created on 5 Aug 2018  路  4Comments  路  Source: python-pillow/Pillow

What did you do?

What did you expect to happen?

What actually happened?

What versions of Pillow and Python are you using?

Please include code that reproduces the issue and whenever possible, an image that demonstrates the issue. Please upload images to GitHub, not to third-party file hosting sites. If necessary, add the image to a zip or tar archive.

The best reproductions are self-contained scripts with minimal dependencies. If you are using a framework such as plone, Django, or buildout, try to replicate the issue just using Pillow.

code goes here

Most helpful comment

from PIL import Image
im = Image.open('test.gif')

For more information, see http://pillow.readthedocs.io/en/5.2.x/handbook/tutorial.html

All 4 comments

from PIL import Image
im = Image.open('test.gif')

For more information, see http://pillow.readthedocs.io/en/5.2.x/handbook/tutorial.html

def decode_gif_file(filename):
    img = Image.open(filename)
    try:
        img.seek(1)
    except EOFError:
        print('Warning: it is a single frame GIF.')
        return img, 1
    return img, 2

# if multiframe
current_index = img.tell() + 1
img.seek(current_index)
# use ur frame with img below. loop current index and judge EOFError to get all frames.

hope it helps

@yo1995 Thanks, you're right, I didn't consider that the question might be about multiple frames

There is also ImageSequence - http://pillow.readthedocs.io/en/5.2.x/reference/ImageSequence.html

from PIL import Image, ImageSequence

im = Image.open("animation.gif")

index = 1
for frame in ImageSequence.Iterator(im):
    frame.save("frame%d.png" % index)
    index += 1

Feel free to comment if you have any further questions

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thinrhino picture thinrhino  路  3Comments

hxzhao527 picture hxzhao527  路  4Comments

vytisb picture vytisb  路  4Comments

dhsdshdhk picture dhsdshdhk  路  4Comments

edowson picture edowson  路  3Comments