How do I programmatically check whether a GIF image is animated?

Pillow 2.9.0 added is_animated:

This adds the property is_animated, to check if an image has multiple layers or frames.

Example usage:

from PIL import Image
print(Image.open("test.gif").is_animated)

With Python and PIL:

from PIL import Image
gif = Image.open('path.gif')
try:
    gif.seek(1)
except EOFError:
    isanimated = False
else:
    isanimated = True

I've never seen a program that will tell you this. But GIF is a block structured format and you can check if the block indicating animated GIF is present in your files.

From wikipedia article noted below: at offset 0x30D an Application Extension (ie: 3 byte magic number 21 FF 0B) block in the GIF file, followed by magic number 4E 45 54 53 43 41 50 45 32 9at offset 0x310 indicates that the rest of the file contains multiple images, and they should be animated.

Really the Wikipedia article explains it better and the format docs noted below expand on the Wiki article.

So you can parse the GIFs using a program written in Python (I parsed GIFs using C many years ago, it was mainly an exercise in moving the file pointer around and reading bytes). Determine if the AE is present with the correct 3 byte ID, and followed by the 9 byte magic number.

See http://en.wikipedia.org/wiki/Graphics_Interchange_Format#Animated_.gif

Also see http://www.martinreddy.net/gfx/2d/GIF87a.txt

Also see http://www.martinreddy.net/gfx/2d/GIF89a.txt

Sorry, best I can do for you.


If you're on Linux (or any system with ImageMagick) you can use a one-liner shell script and identify program:

identify *.gif | fgrep '.gif[1] '

I know you said you prefer PHP and Python, but you also said you are willing to explore other solutions. :)

Tags:

Python

Php

Image