Is there a command to get the number of frames in a gif?
Is there a command to get the number of frames in a gif? I have some gifs, and I want to convert them into arrays, but I want to limit the array, and structure of the array based on the number of frames in any random gif I happen to feed my program. I was thinking imagemagick might have such an option, but i haven't found it yet.
If you have ImageMagick installed, you can use its identify
command as follows
identify /path/to/image.gif
You'll get details of each frame.
As suggested by steeldriver, you can get the number of images in the sequence directly using the %n
output format specifier, however rather annoyingly it appears to print it once for each image. To get a single number you could do
identify -format "%n\n" path/to/file.gif | head -1
Exiftool will list the Frame Count as part of its extensive output.
This a command and output of an Animated Gif file:
$ exiftool -b -FrameCount giphy.gif
33
Full exiftool output on the gif:
$ exiftool giphy.gif
ExifTool Version Number : 10.10
File Name : giphy.gif
Directory : .
File Size : 2.1 MB
File Modification Date/Time : 2018:04:22 21:00:42-04:00
File Access Date/Time : 2018:04:22 21:01:03-04:00
File Inode Change Date/Time : 2018:04:22 21:00:52-04:00
File Permissions : rw-rw-r--
File Type : GIF
File Type Extension : gif
MIME Type : image/gif
GIF Version : 89a
Image Width : 500
Image Height : 281
Has Color Map : Yes
Color Resolution Depth : 7
Bits Per Pixel : 7
Background Color : 127
Animation Iterations : Infinite
XMP Toolkit : Adobe XMP Core 5.3-c011 66.145661, 2012/02/06-14:56:27
Creator Tool : Adobe Photoshop CS6 (Macintosh)
Instance ID : xmp.iid:C312DB1DA6A311E5B8F5D2BA262E999D
Document ID : xmp.did:C312DB1EA6A311E5B8F5D2BA262E999D
Derived From Instance ID : xmp.iid:C312DB1BA6A311E5B8F5D2BA262E999D
Derived From Document ID : xmp.did:C312DB1CA6A311E5B8F5D2BA262E999D
Frame Count : 33
Duration : 2.31 s
Image Size : 500x281
Megapixels : 0.141
One-liner command
Inspired by this article, use this one-liner command:
$ identify drop_caches.gif | wc -l
128
For more details use:
identify -verbose filename.gif
This website lets you upload your .gif
and analyzes it many ways including the number of frames in it:
The convert
provided by Image Magic will convert your .gif
into individual .png
images one for each frame:
$ convert -verbose -coalesce drop_caches.gif drop_caches.png
drop_caches.gif[0] GIF 470x940 470x940+0+0 8-bit sRGB 256c 177KB 0.090u 0:00.099
drop_caches.gif[1] GIF 13x1 470x940+398+704 8-bit sRGB 256c 177KB 0.080u 0:00.089
drop_caches.gif[2] GIF 306x620 470x940+144+130 8-bit sRGB 256c 177KB 0.080u 0:00.089
(... SNIP ...)
drop_caches.gif=>drop_caches-125.png[125] GIF 470x940 470x940+0+0 8-bit sRGB 255c 50.3KB 27.100u 0:24.890
drop_caches.gif=>drop_caches-126.png[126] GIF 470x940 470x940+0+0 8-bit sRGB 254c 48.9KB 27.320u 0:25.089
drop_caches.gif=>drop_caches-127.png[127] GIF 470x940 470x940+0+0 8-bit sRGB 254c 48.9KB 27.480u 0:25.269