What Is a GIF File? How to Check a Graphics Interchange Format Image Before You Trust It
GIF files are Graphics Interchange Format images, and readers that open them can still accept damaged ones. Jump to your question below, or work through it in order.
What Is a GIF File? A 1987 image format that still moves
Short answer
A GIF file is a Graphics Interchange Format image. It starts with GIF87a or GIF89a, and a chain of blocks stores the colors, the frames and the timing. One frame uses at most 256 colors, and one GIF file can store hundreds of frames.
We read 149 GIF files with eight readers, and we damaged one GIF file in 39 ways. All eight readers refused 4 copies, all eight opened 13 and the readers split on 22. Only 7 of the 39 copies came back exact in all four decoders.
To check GIF files without a viewer, read the six steps. To see what is inside, read the layout. If your GIF file will not open, begin with the eight causes.
A GIF file is a Graphics Interchange Format image. The letters GIF89a or GIF87a start it, and a chain of blocks stores colors, frames and timing. The ending is only a label, because the first six bytes decide what the file is.
Picture an image called party.gif that you saved from a chat. It is a still picture or a 3 second loop, and the name does not say which. GIF files can store one image or hundreds of frames, and a reader opens the blocks to find out.
Wikipedia says a CompuServe team led by Steve Wilhite made the GIF file format, and gives 15 June 1987 for version 87a. PRONOM lists the 89a record with a release date of 31 July 1990, while the GIF89a specification names July 1989. We print both dates, because sources differ.
Unisys owned a US patent on LZW, the compression that GIF files use. Wikipedia reports that it expired on 20 June 2003, and that the last counterparts ended in July 2004. Today anyone can write GIF files without a license.
Endings cause the first mix up. We copied one GIF file to 10 names, like picture.jpg, notes.txt and a name with no ending. All eight readers still read the GIF file in all 10, so the first bytes decide what the file is.
The reverse also works. Six other files got the ending .gif, from PNG and JPEG to MP4 and WebP. The file command, MediaInfo and ffprobe named the true type of all six, and giftext and gifsicle refused them as GIF files.
Who makes GIF files
Image libraries and their test suites write the GIF files in our set. Our 149 GIF files come from the test folders of 5 public projects. They are libgd with 51, Pillow with 40, ImageMagick with 33, the Rust crate image gif with 23 and gifsicle with 2.
Many are built to be odd, so they include cases that an export from a normal editor does not.
We fetched 160 files and dropped 11 byte identical copies. The 149 files total 13,299,277 bytes and range from an empty file to 10,130,100 bytes, with a median of 530 bytes. We recorded the SHA256 hash of every file. The tests below also use 39 damaged copies of one GIF file, and 10 renamed copies.
GIF file facts at a glance
| Property | Value |
|---|---|
| Meaning | Graphics Interchange Format. GIF files store one image or a series of frames as a chain of blocks. |
| Ending |
.gif. The media type is image/gif, and the IANA image registry lists it with RFC 2045 and RFC 2046.
|
| Made by | A CompuServe team. Wikipedia gives 15 June 1987 for version 87a, and PRONOM lists version 89a as 31 July 1990. |
| First bytes |
47 49 46 38 39 61 (GIF89a) in 131 of our 146 GIF files with a signature, and 47 49 46 38 37 61 (GIF87a) in 15.
|
| Colors | GIF files use up to 256 colors for one frame, chosen from a 24 bit space, per Wikipedia. A color table has 2, 4, 8, 16, 32, 64, 128 or 256 entries. |
| Frames | 84 of our GIF files have one image and 59 have two or more. The busiest has 380 frames. |
| Opens with | Microsoft Photos, Paint, IrfanView, GIMP, Apple Preview and Safari, per FileInfo. We tested eight readers and four decoders on Linux. |
| Our tests | 149 GIF files, 39 damaged copies and 10 renamed copies, read by eight readers and four decoders. |
Wrong page? A file that ends in .apng, .webp or .png is a different format. Compare them by their first bytes.
What Is Inside a GIF File? A 6 byte header and a chain of blocks
Every GIF file has a 6 byte header, a 7 byte screen descriptor, an optional color table and then blocks until a one byte trailer, 3b. In our 47,768 byte example the first image starts at byte 808, and the trailer is byte 47,767.
We made a 2 second test clip, testsrc.gif, so every byte can be read. This GIF file is 47,768 bytes long and has 20 frames of 160 by 120 pixels. The figure shows the blocks from the outside in.
Read the figure from the top. Six letters at byte 0 are the only fixed check in any GIF file, and 47 49 46 38 39 61 spells GIF89a. Byte 6 starts the screen descriptor with a width of a0 00 and a height of 78 00. The low byte comes first, so they read 160 and 120.
The packed byte f7 says a global color table follows, with 256 colors. That table takes 768 bytes, 3 for every color, and ends at byte 780.
11 of our 146 GIF files have no global table at all. The background index is 255, and the aspect byte is 49. The specification gives the ratio as the byte plus 15, divided by 64, so 49 means 1 to 1.
The loop block and the graphic control block
Two extension blocks come before the first image of this GIF file. Both start with 21, the extension introducer, and the next byte names the kind. The first one stores the loop count.
xxd -g 1 -s 781 -l 19 testsrc.gif
0000030d: 21 ff 0b 4e 45 54 53 43 41 50 45 32 2e 30 03 01 !..NETSCAPE2.0..
0000031d: 00 00 00 ...
21 ff 0b opens an application block of 11 bytes, and the next 11 bytes spell NETSCAPE2.0. Then 03 01 00 00 says a sub block of 3 bytes follows with a loop count of 0, which means loop forever. Wikipedia says Netscape Navigator 2.0 added looping this way in September 1995. 43 of our GIF files have the block.
The graphic control block comes next. It sets the delay, disposal method and transparent color for the image after it. 116 of our 146 GIF files have at least one block like it.
xxd -g 1 -s 800 -l 8 testsrc.gif
00000320: 21 f9 04 05 0a 00 3f 00 !.....?.
21 f9 04 opens a block of 4 bytes. The packed byte 05 says disposal method 1 and a transparent color. 0a 00 is the delay, 10 hundredths of a second or 100 ms, and 3f is the transparent index, 63. The last byte, 00, ends the block.
The image blocks and the pixel data
Every image starts with 2c, the image separator. A 10 byte descriptor follows with the left and top position, the size and a flag byte. The first image of testsrc.gif has its own table of 64 colors, code size 8 and 3,192 bytes of data in 13 sub blocks.
The pixel data of GIF files is compressed with LZW. Sub blocks store 1 to 255 bytes behind a length byte, and a length of 0 ends the data. An interlaced image stores its rows in four passes, and 9 of our 146 files use that flag.
The fields of GIF files, one by one
| Field | What we found |
|---|---|
| Header | Six bytes, GIF87a or GIF89a. 131 of our 146 files with a signature say 89a and 15 say 87a. |
| Screen size | Two bytes for the width and two for the height. A 40 byte file in our set claims 6,727 by 65,351 pixels. |
| Global table | 2 to 256 colors at 3 bytes per color. 135 of 146 files have one. 49 have 2 colors, 39 have 256, 20 have 4 and 15 have 8. |
| Graphic control | The delay, the disposal method and the transparent index. 116 of 146 files have at least one block, and 50 use a transparent color. |
| Application block | 42 files name NETSCAPE2.0 and 1 names NETSCAPE2.1. A few others name ImageMagick, XMP data or a color profile. |
| Comment block | 15 of 146 files have one. It stores text and no pixel. |
| Image block | Position, size and a flag byte. 30 files have a local color table. |
| Trailer |
One byte, 3b. One file has no trailer, and one has 47 extra bytes after it.
|
The header is small. Our check script reads 13 header bytes and 8 blocks. It decodes no pixel, so damage deeper in the GIF file can pass it, as the damage tests show.
Which Kind of GIF File Do You Have? Version, frames and color table, and the ending names none
Three questions sort GIF files, the version, the number of images and the size of the color table. Our 146 files with a signature gave 131 GIF89a and 15 GIF87a. 84 have one image and 59 have more, and 49 have a 2 color table against 39 with 256.
Most questions about GIF files come down to what is inside. Is it a still picture or a loop? Does it need transparency? The header and the first blocks answer all three, and no pixel has to be decoded to read them.
GIF87a or GIF89a
The header names a version and says little about what the GIF file uses. The specification asks encoders to use the earliest version that covers every block in the file. 5 of our 15 GIF87a files still contain blocks that only GIF89a defines, like comments or graphic control blocks.
So the version of GIF files is only a label, and the blocks are the facts. Any file that says 87a and has graphic control blocks is GIF89a in practice. In our damage test, a copy with 87a over 89a blocks passed all eight readers.
Still pictures and animations
84 of our GIF files have one image, 59 have two or more and 3 have none. The busiest has 380 frames. 43 files have a loop block, and 36 of them loop forever. The other 7 loop a set number of times.
Palettes and transparency
Frames in GIF files store palette indexes, not colors. Wikipedia says one frame uses at most 256 colors. Small tables are common here, because 69 of our 135 global tables have 2 or 4 colors. 30 files add a local table to frames, and 50 use a transparent index.
Not sure the file is a GIF file at all? Check it with our script.
What Did We Find in 149 GIF Files? Eight readers and four decoders, with gaps on odd files
We read 149 GIF files from 5 public projects with eight readers. MediaInfo, file and gifsicle opened 147. ImageMagick and giftext opened 130. Four decoders returned every frame of 116 of the 132 files that reach the trailer. The set has more odd cases than a normal export does.
We fetched 160 files from the test folders of libgd, Pillow, ImageMagick, the Rust crate image gif and gifsicle. We dropped 11 byte identical copies, which left 149 GIF files. We recorded the SHA256 hash of every file and checked it again at the end.
The readers were the file command 5.45, MediaInfo 24.12, hachoir 3.4.0, ffprobe 6.1.1, Pillow 12.3.0, ImageMagick 6.9.12, giftext from giflib 5.2.2 and gifsicle 1.94. The decoders were ImageMagick, Pillow, ffmpeg 6.1.1 and OpenCV 5.0.0.93, on Python 3.13 and Linux. We displayed no GIF file on a screen.
A reader opened a GIF file when it parsed the file and named its type without an error. That test is light, because Pillow reads only the header and the file command reads the first letters. A decoder test is stronger, since it returns frames.
| Test | Result |
|---|---|
| Opened | file, MediaInfo and gifsicle opened 147 of 149 GIF files. ffprobe opened 143, hachoir 140, Pillow 137, ImageMagick 130 and giftext 130. |
| Decoded | Four decoders read the 132 GIF files that reach the trailer. ImageMagick returned every frame of 129, Pillow of 128, ffmpeg of 124 and OpenCV of 120. All four did so for 116. |
| Version | 131 files say GIF89a, 15 say GIF87a and 3 have no signature. 5 of the 15 GIF87a files use blocks that only GIF89a defines. |
| Images | 84 files have one image, 59 have two or more and 3 have none. The busiest has 380 frames. |
| Color tables | 135 files have a global table, 49 of them with 2 colors and 39 with 256. 30 files have a local table. |
| Extras | 50 files use transparency, 43 have loop blocks, 15 have comments and 9 are interlaced. |
| Sizes | From an empty file to 10,130,100 bytes. The median is 530 bytes, and the 149 files total 13,299,277 bytes. |
All eight readers opened 125 of the 149 GIF files. One file was opened by none, and the other 23 split the readers. Every split comes from a file built to be odd, as the next parts show.
The 17 files that do not reach the trailer
Our walker reads every block from header to trailer. 132 GIF files reach the trailer and 17 do not. Seven hit a byte that is no block, six end inside the image data, two are too short, one has no trailer and one has the version GIF90a.
Their names say why these GIF files stop early. The libgd folder has bad_magic, empty, no_trailer, truncated_header and truncated_lzw, and the Pillow folder has zero_width and decompression_bomb. These are test cases on purpose.
The file that no reader opened
libgd_empty has 0 bytes, and all eight readers refused it. libgd_truncated_header has 4 bytes, the letters GIF8. Only file and gifsicle named it a GIF file. libgd_bad_magic says GIF90a, and MediaInfo, giftext and gifsicle opened it while the other five refused.
Why ImageMagick and giftext opened fewer
ImageMagick and giftext both refused 19 GIF files. Most are among the 17 above. ImageMagick also refused four files that reach the trailer. They are libgd_bug00005_2, libgd_bad_lzw_code, libgd_zero_dimensions and libgd_unitialized_memory_read. giftext refused three more that reach the trailer, pillow_hopper_zero_comment_subblocks, pillow_issue_2811 and image-gif_set_hsts.
So a refusal from strict tools is a signal, not a verdict. Ask another reader before you call the GIF file damaged.
Where the four decoders disagree
libgd_zero_dimensions failed in all four decoders. libgd_bug00005_2 and libgd_bad_lzw_code failed in ImageMagick, Pillow and OpenCV, and ffmpeg returned their frames. OpenCV missed 12 of the 132 files, including pillow_background_outside_palette.
Read the study with care. It shows what eight readers and four decoders do with 149 GIF files from 5 public test folders. Many are test files, so the odd cases are common here. A reader that agrees on our GIF files can still fail on yours.
What Do Readers Do With a Damaged GIF File? 39 broken copies, eight readers and 22 split decisions
We damaged one 47,768 byte GIF file in 39 ways. All eight readers refused 4 copies, all eight opened 13 and the readers split on 22. Opened copies by reader ranged from 18 for giftext to 35 for MediaInfo. Only 7 copies came back exact in all four decoders.
The 39 damaged copies of one GIF file, testsrc.gif, come in seven groups. Group A damages header fields, B the first bytes and C cuts the file short. D changes extension blocks, E image fields, F pixel data and G adds data around the trailer. Every copy changes one thing.
We asked two questions about every damaged GIF file. Did the eight readers open it? And how many of its 20 frames did a decoder return that equal the intact frame? The second question is the strict one.
The 4 copies that every reader refused
Copies A1, A3, B1 and B2 broke the one fixed check. Their damage is the signature GIX89a, a zeroed signature or the first 6 or 13 bytes removed. All eight readers refused all four damaged GIF files, and our script says the first 6 bytes are not a GIF signature.
The 13 copies that every reader opened
Most header damage in GIF files went unseen. Version 87a on a file with GIF89a blocks and pixel aspect byte 255 passed every reader. So did copies with the loop block removed, every delay set to 0 or 65535 and 64 KB of random bytes after the trailer. In all four decoders those came back with all 20 frames exact.
Opened does not mean an identical picture. Six of the 13 GIF files gave frames that differ from the intact file.
They set the canvas to 640 by 480 or 8 by 8, use disposal method 7, move a frame by 400 pixels or flag a frame as interlaced. One more copy adds a second copy of the file after the trailer, and that made ffmpeg return 40 frames.
The 22 copies that split the readers
giftext opened only 18 of the 39 damaged GIF files and ImageMagick 20, fewer than any other reader. MediaInfo opened 35.
A cleared global table flag and a table size of 2 colors were opened by file, MediaInfo, hachoir and ffprobe, and refused by Pillow, ImageMagick, giftext and gifsicle. A graphic control block of 5 bytes split the readers the same way.
The cut GIF files split the decoders. A copy cut in half gave 9 frames in Pillow, 10 in ffmpeg and none in ImageMagick or OpenCV. A copy with the trailer byte removed gave all 20 frames in three decoders and none in OpenCV. OpenCV was the strictest decoder, with 8 exact copies against 14 for ImageMagick.
Readers opened copies that have no correct frame
Copies E5 and E6 change the code size of the first frame from 8 to 1 or 9, and F1 sets 8 bytes of its data to ff. Six or seven of the eight readers opened every one. No decoder returned a correct frame. ffmpeg returned 20 frames of wrong pixels, and the others returned none.
A reader that opens a GIF file shows that the blocks parse. It does not show that the picture decodes. For anything you rely on, decode it and compare a frame, as the frame count shows.
Read the grid with care. Every copy changes one thing in one GIF file of 20 frames. Damage in the wild changes many things at once. We compared frames pixel by pixel with the intact file and looked at no browser or viewer, so the grid shows how readers answer.
What Does Our Expert Say About GIF Files? A note from Neeraj Singh on testing before you give up
Check GIF files with two tools before you trust one answer. The eight readers opened 125 of 149 files together and split on 22 of 39 damaged copies. A header check takes seconds, and a frame comparison takes a little longer.
Expert insight
“GIF files look too simple to break, so people trust the first viewer that shows a picture. In our tests eight readers split on 22 of 39 damaged copies, and only 7 copies came back exact in all four decoders. Check every GIF file with two readers, compare one frame and keep the original.”
Why a quiet reader proves little
A reader that opens the GIF file shows that the header and the blocks parse. It does not show that the pixels decode. In our tests six readers opened a copy with broken code size, and no decoder returned a correct frame.
The check has a limit. Two readers that agree on a GIF file can both be wrong, and neither shows you the animation. The script from the check reads 13 bytes and 8 blocks, so compare its answer with another reader and one decoded frame.
Copy the GIF file before you touch it. Write down the SHA256 hash of the original and work on a copy. The hash shows which file you received, and it lets you spot a changed download later.
How Do You Open a GIF File? Any image viewer works, and readers disagree on odd files
Most image viewers open GIF files, and FileInfo lists Photos, Paint, IrfanView, GIMP, Preview and Safari. We read all 149 GIF files with eight readers and four decoders instead. They open 130 to 147 files, and the gaps come from odd files.
The table lists the programs that open GIF files. We used some of them, and FileInfo names the rest. It marks what we tested, because we worked on Linux and displayed no GIF file on a screen.
| Program | What it does |
|---|---|
| Viewers and editors | FileInfo lists Microsoft Photos, Paint, IrfanView and Photoshop for Windows. It lists Apple Preview and Safari for Mac and GIMP for Linux, per the FileInfo list. We played no file, so we tested none of them. |
| ffprobe and ffmpeg | Version 6.1.1. ffprobe opened 143 of 149 GIF files, and ffmpeg returned every frame of 124 of the 132 that reach the trailer. |
| MediaInfo | Version 24.12 opened 147 GIF files and named the format GIF. |
| Pillow | A Python library, version 12.3.0. It opened 137 files and returned every frame of 128. |
| ImageMagick | Version 6.9.12. It opened 130 files and returned every frame of 129, the best score of the four decoders. |
| gifsicle |
Version 1.94. It opened 147 files and prints the blocks with the option --info.
|
| OpenCV | A video library, version 5.0.0.93. It returned every frame of 120 of the 132 files, the strictest of the four. |
| Our script | A 69 line Python script that reads 13 bytes and the first 8 blocks. It decodes nothing. |
Open GIF files with ffprobe
The ffprobe tool is part of FFmpeg. It prints the streams of the GIF file and decodes nothing. For our example it names a GIF stream of 160 by 120 pixels.
ffprobe -v error -show_entries stream=codec_name,width,height -of csv=p=0 testsrc.gif
gif,160,120
To count the frames of GIF files, add the option -count_frames. That option decodes every frame, so it is the read here that proves the pictures are there. Our example gave 20 frames.
ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of csv=p=0 testsrc.gif
20
Open GIF files with gifsicle and ImageMagick
The gifsicle tool prints the blocks of a GIF file, one image at a time. Its option --info shows the screen size, color table, loop and delay of every image. ImageMagick lists frames with identify.
gifsicle --info testsrc.gif
* testsrc.gif 20 images
logical screen 160x120
global color table [256]
background 255
loop forever
+ image #0 160x120 transparent 63
local color table [64]
disposal asis delay 0.10s
+ image #1 160x98 transparent 255
disposal asis delay 0.10s
+ image #2 159x94 at 1,4 transparent 255
disposal asis delay 0.10s
identify testsrc.gif
testsrc.gif[0] GIF 160x120 160x120+0+0 8-bit sRGB 64c 0.000u 0:00.000
testsrc.gif[1] GIF 160x98 160x120+0+0 8-bit sRGB 256c 0.000u 0:00.000
testsrc.gif[2] GIF 159x94 160x120+1+4 8-bit sRGB 256c 0.000u 0:00.000
ImageMagick opened 130 files, fewer than most readers did. Read why strict readers refuse files before you call a refused file damaged.
Viewers and converters for GIF files
These four links open outside pages for GIF files. Three come from SysTools and one from Freeviewer. We describe them from their own pages and tested none of them.
SysTools calls its GIF viewer a standalone tool that plays GIF animations, with play, pause, zoom and forward and backward controls. Its page says it saves frames as PNG, TIFF or JPEG and shows frame number, delay and duration. The page also says it does not repair a corrupt GIF file.
The SysTools GIF converter writes PDF, DOC, DOCX, HTML, TXT and image formats like APNG, PNG, TIFF, WEBP and JPEG. Its page says a free demo converts the first 5 files.
The SysTools Image Converter lists GIF among its inputs on Windows and on Mac, and its page lists a demo with a limit as well. Check the current terms on its page.
Freeviewer's page describes an image viewer for Windows that lists GIF, PNG, JPEG, TIFF, BMP and ICO among its formats. It previews images, reorders them and prints them. Its page says the free version exports the first 5 images. We list no prices here, because they change.
Install a viewer from its maker. Download a viewer from the maker's own page, and write down the file hash if the page gives one. GIF files do not need special software, and our eight readers worked without any.
How Do You Check a GIF File? Six steps, and only one of them decodes frames
Check GIF files in six steps. Read the first 6 bytes, the screen descriptor and the first block, then read the blocks with our 69 line script. Only the last step decodes frames. The script reads 13 bytes and 8 blocks and then stops.
The first reads need no Python. The xxd command prints bytes, and the file command names the type of every GIF file. For testsrc.gif the reads below show the letters GIF89a, a canvas of 160 by 120 and a global table of 256 colors.
xxd -g 1 -l 13 testsrc.gif
00000000: 47 49 46 38 39 61 a0 00 78 00 f7 ff 31 GIF89a..x...1
file -b testsrc.gif
GIF image data, version 89a, 160 x 120
Read the first line from the left. Bytes 0 to 5, 47 49 46 38 39 61, spell GIF89a. Next, a0 00 is 160 and 78 00 is 120. Packed byte f7 means a global table of 256 colors. Background index ff is 255, and aspect byte 31 is 49.
Step 4 starts at byte 13 plus the size of the color table. For a table of 256 colors that is byte 781, where the loop block began in the loop block layout.
Check GIF files with Python
The script reads the 13 header bytes of GIF files and then up to 8 blocks. It prints the version, screen size and color table, then every block with its delay, disposal method and size. It never decodes a pixel, and it never changes the file.
#!/usr/bin/env python3
"""gif_peek.py - read the header and the first blocks of a GIF file and print what they say. Decodes no pixel.
Usage: python3 gif_peek.py FILE [FILE ...]
"""
import os, struct, sys
def chain(f, keep=16):
"""Step over a chain of sub-blocks. Return (first bytes, total size), or None if the file ends first."""
head, total = b"", 0
while True:
n = f.read(1)
if not n: return None
if n[0] == 0: return head, total
part = f.read(n[0])
if len(part) < n[0]: return None
head, total = (head + part)[:keep], total + n[0]
def peek(path, limit=8):
name = os.path.basename(path)
with open(path, "rb") as f:
head = f.read(13)
if head[:6] not in (b"GIF87a", b"GIF89a"):
return "%s: not a GIF file, the first 6 bytes are %s" % (name, head[:6].hex(" ") or "missing")
w, h, flags = struct.unpack("<HHB", head[6:11])
colors = 2 << (flags & 7)
out = ["%s: %s, %d x %d, %s" % (name, head[:6].decode(), w, h,
"global table of %d colors" % colors if flags & 128 else "no global table")]
if flags & 128:
if len(f.read(3 * colors)) < 3 * colors:
return "\n".join(out + ["%s: the file ends inside the global table" % name])
for n in range(1, limit + 1):
kind = f.read(1)
if not kind:
out.append("%s: the file ends after %d blocks, with no trailer" % (name, n - 1)); break
if kind == b"\x3b":
out.append("%s: block %d is the trailer, then %d extra bytes" % (name, n, len(f.read()))); break
if kind == b"\x21":
label, body = f.read(1), chain(f)
if body is None or not label:
out.append("%s: block %d is an extension that ends too early" % (name, n)); break
data, size = body
if label == b"\xf9" and size == 4:
what = "graphic control, delay %d ms, disposal %d%s" % (
struct.unpack("<H", data[1:3])[0] * 10, data[0] >> 2 & 7, ", transparent" if data[0] & 1 else "")
elif label == b"\xff":
what = "application " + data[:11].decode("latin1")
if data[:8] == b"NETSCAPE": what += ", loops %d" % struct.unpack("<H", data[12:14])[0]
else:
what = {b"\xfe": "comment", b"\x01": "plain text"}.get(label, "extension %s" % label.hex())
out.append("%s: block %d is %s, %d bytes" % (name, n, what, size))
elif kind == b"\x2c":
desc = f.read(9)
if len(desc) < 9:
out.append("%s: block %d is an image cut short" % (name, n)); break
x, y, iw, ih, fl = struct.unpack("<HHHHB", desc)
if fl & 128: f.seek(3 * (2 << (fl & 7)), 1)
f.read(1)
body = chain(f)
if body is None:
out.append("%s: block %d is an image whose data ends too early" % (name, n)); break
out.append("%s: block %d is an image at %d,%d, %d x %d, %s%s, %d bytes of data" % (
name, n, x, y, iw, ih, "local table" if fl & 128 else "global table", ", interlaced" if fl & 64 else "", body[1]))
else:
out.append("%s: block %d starts with %s, which is no block" % (name, n, kind.hex())); break
return "\n".join(out)
if __name__ == "__main__":
if len(sys.argv) < 2: sys.exit(__doc__)
for p in sys.argv[1:]: print(peek(p))
Here is what it printed for three GIF files. They are our test clip, a 14 frame animation that loops 10 times and a file with a comment block.
python3 gif_peek.py testsrc.gif image-gif_moon_impact.gif pillow_chi.gif
testsrc.gif: GIF89a, 160 x 120, global table of 256 colors
testsrc.gif: block 1 is application NETSCAPE2.0, loops 0, 14 bytes
testsrc.gif: block 2 is graphic control, delay 100 ms, disposal 1, transparent, 4 bytes
testsrc.gif: block 3 is an image at 0,0, 160 x 120, local table, 3192 bytes of data
testsrc.gif: block 4 is graphic control, delay 100 ms, disposal 1, transparent, 4 bytes
testsrc.gif: block 5 is an image at 0,0, 160 x 98, global table, 2383 bytes of data
testsrc.gif: block 6 is graphic control, delay 100 ms, disposal 1, transparent, 4 bytes
testsrc.gif: block 7 is an image at 1,4, 159 x 94, global table, 2383 bytes of data
testsrc.gif: block 8 is graphic control, delay 100 ms, disposal 1, transparent, 4 bytes
image-gif_moon_impact.gif: GIF89a, 116 x 100, global table of 256 colors
image-gif_moon_impact.gif: block 1 is application NETSCAPE2.0, loops 10, 14 bytes
image-gif_moon_impact.gif: block 2 is graphic control, delay 150 ms, disposal 0, 4 bytes
image-gif_moon_impact.gif: block 3 is an image at 0,0, 116 x 100, global table, 11613 bytes of data
image-gif_moon_impact.gif: block 4 is graphic control, delay 150 ms, disposal 0, 4 bytes
image-gif_moon_impact.gif: block 5 is an image at 0,0, 116 x 100, local table, 11613 bytes of data
image-gif_moon_impact.gif: block 6 is graphic control, delay 150 ms, disposal 0, 4 bytes
image-gif_moon_impact.gif: block 7 is an image at 0,0, 116 x 100, local table, 11613 bytes of data
image-gif_moon_impact.gif: block 8 is graphic control, delay 150 ms, disposal 0, 4 bytes
pillow_chi.gif: GIF89a, 320 x 240, global table of 256 colors
pillow_chi.gif: block 1 is application NETSCAPE2.0, loops 0, 14 bytes
pillow_chi.gif: block 2 is comment, 17 bytes
pillow_chi.gif: block 3 is graphic control, delay 100 ms, disposal 0, transparent, 4 bytes
pillow_chi.gif: block 4 is an image at 0,0, 320 x 240, global table, 1110 bytes of data
pillow_chi.gif: block 5 is graphic control, delay 100 ms, disposal 0, transparent, 4 bytes
pillow_chi.gif: block 6 is an image at 0,0, 320 x 240, global table, 1267 bytes of data
pillow_chi.gif: block 7 is graphic control, delay 100 ms, disposal 0, transparent, 4 bytes
pillow_chi.gif: block 8 is an image at 0,0, 320 x 240, global table, 1342 bytes of data
The first line of every answer gives the version, size and table. Blocks say whether they are graphic control, application, comment or image. Damaged files print a reason, and the next listing shows four of them.
python3 gif_peek.py A1.gif B2.gif C2.gif D2.gif
A1.gif: not a GIF file, the first 6 bytes are 47 49 58 38 39 61
B2.gif: not a GIF file, the first 6 bytes are 00 00 3e 00 00 fe
C2.gif: GIF89a, 160 x 120, global table of 256 colors
C2.gif: the file ends inside the global table
D2.gif: GIF89a, 160 x 120, global table of 256 colors
D2.gif: block 1 is application NETSCAPE2.0, loops 0, 14 bytes
D2.gif: block 2 is extension f9, 8107 bytes
D2.gif: block 3 starts with 92, which is no block
What the script found in our GIF files
| Result | Cases |
|---|---|
| Read (132 of 149) | The script read 132 GIF files, the same 132 that reach the trailer in our walker. |
| Refused (17 of 149) | It refused the 17 files that stop early, with a reason like "the file ends inside the global table". |
| Refused copies (14 of 39) | It refused the five GIF files that break the header, A1, A2, A3, B1 and B2. It also refused the three cut short, C1 to C3. It refused A9, A10, D2, E3, F3 and F4 as well. |
| Passed with damage (25 of 39) | 18 of the 25 came back wrong in at least one decoder, because the script reads no pixel. 4 of them, A7, E5, E6 and F1, gave no correct frame. |
| Files changed (0) | The SHA256 hash of all 149 files matched the hash we took before the tests. |
The script reads blocks and nothing else. It does not decode a pixel, compare a frame or check the length of every sub block. GIF files with broken pixel data can pass it, so the damaged copies matter.
Check order for any GIF file
- Copy the GIF file, write its SHA256 hash and work on the copy.
-
Read the first 6 bytes. They must be
47 49 46 38 39 61(GIF89a) or47 49 46 38 37 61(GIF87a). - Read bytes 6 to 12. The width and the height come first, and the packed byte says whether a global color table follows.
-
Read the byte after the color table. A value of
21opens an extension,2copens an image and3bends the file. - Read the blocks with our script. It names the first 8 blocks and says where a block stops making sense.
- Compare with another reader and count the frames with ffprobe. Only this step decodes, and a difference means damage or a reader limit.
Is a GIF File Safe to Open? The file is data, but the name and the canvas size can mislead
GIF files are data that viewers read, so reading one with a reader is safe. A name proves nothing, because renamed copies still read as GIF files. Small files can claim huge canvases, and the decoders we used refused those.
We displayed none of our 149 GIF files and scanned none for malware. All eight readers only read bytes, and the SHA256 hash of every file matched after every test. The files come from public test folders, so their contents are not the worry on this page.
The rename test
We copied one GIF file to 10 names, like base.jpg, base.mp4, base.txt, base.docx, a name with no ending and base.gif.exe. All eight readers read all 10 as the same GIF file. The ending is a label, and the first bytes decide what a file is.
The reverse also works. Six other files got the ending .gif. The file command, MediaInfo and ffprobe named the true type of all six. Pillow and ImageMagick named five, and hachoir named four. giftext and gifsicle refused all six, because the letters GIF are missing.
If your file manager hides endings, base.gif.exe can look like a picture. Show full file names, and read the first bytes of anything you doubt.
Small files with huge canvases
The screen descriptor gives the width two bytes and the height two bytes, so GIF files of 40 bytes can ask for hundreds of millions of pixels. One test file from the Rust crate does that. Pillow raised a DecompressionBombError, and ImageMagick, ffmpeg and OpenCV returned no frame.
Keep your tools up to date, and do not open GIF files of unknown origin in a program that has no size limit. We tested only the four decoders above, and we make no claim about a viewer.
What a comment block shows
A comment block is a string of text sub blocks. In our GIF files 15 of 146 have one, and our script prints its length. We read none of them as a program.
Not sure? Check it first. Copy the hash, read the header with our script and compare another reader. If the GIF file came in an email you did not expect, delete the message and ask the sender through another route.
Why Will Your GIF File Not Open? Eight causes, and what to do about every one
Most GIF files that will not open fail for one of eight reasons. Read the first bytes first, then the blocks, then count the frames. Our eight readers refused 4 of 39 damaged copies and opened 13 that still came back wrong in a decoder.
We read 149 GIF files and 39 damaged copies with eight readers and four decoders. The table groups what we saw into eight causes. The number in brackets matches the cause.
Every row says what happens to the GIF file and what to try. The fixes work on a copy, and none changes the original. A reader that says nothing is no sign that the picture decodes.
| Cause | Result and fix |
|---|---|
| A damaged header (1) | All eight readers refused 4 of our 39 damaged GIF files, the ones with a bad signature or the first 6 or 13 bytes removed. Fix: Download the file again and compare the SHA256 hash. We tested no repair. |
| A cut download (2) | A GIF file cut in half was opened by 6 of 8 readers, but Pillow returned 9 frames of 20 and ffmpeg 10. ImageMagick and OpenCV returned none. Fix: Compare the size and the hash with the source, then download again. |
| A file that is not GIF (3) |
A PNG, JPEG, BMP, MP4 or WebP file named .gif has no GIF letters at byte 0, and giftext and gifsicle refused all six we made. Fix: Read the first 6 bytes. If they are not 47 49 46 38, name the type with the file command and open it as that type.
|
| Broken pixel data (4) | Changing the code size or 8 bytes of data left the GIF file readable in 6 or 7 readers and gave no correct frame. Fix: Count the frames with ffprobe, then ask the source for a new export. We tested no repair. |
| A wrong canvas or frame field (5) | GIF files with a canvas of 640 by 480, an offset of 400 or an interlace flag opened in all eight readers, and the frames came back different. Fix: Read the screen size and the frame positions with our script and compare them with what you expect. |
| A strict reader (6) | ImageMagick and giftext both refused 19 of 149 GIF files, and four of the ImageMagick refusals reach the trailer. Fix: Try another reader like ffmpeg or Pillow before you call the file damaged. |
| Odd delays or no loop (7) |
GIF copies with every delay at 0 or 65535, or with the loop block removed, opened in all eight readers and decoded all 20 frames. Fix: Print the delays and the loop with gifsicle --info, and ask the source for a new export.
|
| A huge canvas (8) | A 40 byte GIF file in our set claims 6,727 by 65,351 pixels, and all four decoders refused it. Fix: Read the screen size with our script before you open a file of unknown origin. |
Check GIF files that will not open, step by step
We tested the reading steps on Python 3.13 and Linux. We displayed no GIF file and tested no viewer, and the steps keep the original untouched at every point.
- Copy the GIF file, write its SHA256 hash and work on the copy.
- Read the first 6 bytes with the check steps. If they are not GIF, name the type with the file command.
- Read the blocks with the header script. A refusal names the block where the file stops making sense.
- Count the frames with the ffprobe command. Readable blocks and 0 frames point to broken pixel data.
- If the file is smaller than the source says, download it again and compare the hash.
- If one tool refuses the file, try another reader before you decide, as the study shows.
- If you need another format, copy the frames or convert them, as the convert part shows.
What to send a helper
Short messages get faster answers. Include the details below, and add the script output. Its first line gives the version, size and color table, and later lines name every block.
Subject: GIF file will not open
File: name, size in bytes and SHA256 hash
Header: the output of gif_peek.py
Frames: the count from ffprobe
System: viewer name, version and system
What happens: the exact words of the error
Source: where the GIF file came from and when it was copied
What no step can fix
Do not try online repair sites. A site that offers to fix any GIF file for free can keep the upload. Try the steps above on a copy first.
Can You Convert a GIF File? MP4 was smaller for 14 of 50 animations
Animated GIF files copy into MP4, APNG and WebP with the same frame count. MP4 was smaller for 14 of 50 files, and for 7 of the 9 files of 20,000 bytes or more. GIF files with 256 colors cannot store the 9,684 colors of a photo, so the picture changes when it is saved.
Converting GIF files means writing its frames in another format. The table lists what we found for 115 GIF files that all four decoders read in full, 50 animated and 65 still. Every row names its source.
| Type | Finding and source |
|---|---|
| GIF to MP4 | ffmpeg with libx264 wrote an MP4 for all 50 animated GIF files, and every MP4 had the same frame count. The MP4 was smaller for 14 of 50, and for 7 of the 9 files of 20,000 bytes or more. Source: Our tests. |
| GIF to APNG | ffmpeg wrote an APNG for all 50 animated GIF files, with the same frame count every time. The APNG was smaller for 12 of 50. Source: Our tests. |
| GIF to WebP | gif2webp wrote a WebP for 49 of 50 animated GIF files, lossless and lossy. The lossy files kept the frame count for 45. The WebP was smaller for 16 of 49 lossy and 19 of 49 lossless. Source: Our tests. |
| GIF to PNG | The first frame became a PNG with the same pixels for all 115 files. The PNG was smaller than the GIF file for 18 of 65 still pictures. Source: Our tests. |
| GIF to a smaller GIF |
gifsicle with the option -O3 made 73 of 115 GIF files smaller, left 35 the same size and made 7 bigger. 103 kept every frame pixel for pixel in our check. Source: Our tests.
|
Convert GIF files to MP4
The pad filter rounds the picture size up to even numbers, which yuv420p needs. The option -an drops any sound track, and GIF files never have one. For testsrc.gif the MP4 has 23,701 bytes against 47,768 for the GIF file, and ffprobe counts 20 frames in both.
ffmpeg -i testsrc.gif -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2:0:0:color=black,format=yuv420p" -c:v libx264 -crf 23 -an -movflags +faststart testsrc.mp4
file -b testsrc.mp4
ISO Media, MP4 Base Media v1 [ISO 14496-12:2003]
Small GIF files do not shrink. For the 41 animated files under 20,000 bytes the MP4 was smaller only 7 times. The two big exceptions, a file with 259 frames and a file with 380, grew from 751,774 and 356,707 bytes to 1,149,191 and 460,381 bytes.
Convert GIF files to APNG or WebP
The commands below write the other two formats. For testsrc.gif the APNG has 49,976 bytes, which is more than the GIF file, and the lossless WebP has 31,362. That WebP file has 20 frames, like the original.
ffmpeg -i testsrc.gif -f apng -plays 0 testsrc.apng
gif2webp testsrc.gif -o testsrc.webp
Make GIF files smaller with gifsicle
The gifsicle tool rewrites the blocks of GIF files and keeps their colors. For testsrc.gif the option -O3 gave 42,654 bytes, and all 20 frames stayed identical. The check is worth doing, because one of our files lost a frame.
gifsicle -O3 testsrc.gif -o testsrc.opt.gif
The Pillow check flagged 12 of the 115 optimized GIF files. ImageMagick and ffmpeg agreed with the original for 7 and 9 of those 12. Output from pillow_duplicate_frame had 2 frames in ImageMagick against 3 in the original.
What happens to the colors
We saved a 128 by 128 photo with 9,684 colors, hopper.png from the Pillow folder, as GIF files at four palette sizes. The table shows file size and how far pixels move from the source. This photo is 30,216 bytes as PNG, 21,572 as lossless WebP and 7,173 as JPEG at quality 90.
| Palette | Result |
|---|---|
| 256 colors | 16,479 bytes in Pillow. The average error is 2.81 of 255 per color channel, and the worst is 53. |
| 64 colors | 9,488 bytes. The average error is 4.77 and the worst is 72. |
| 16 colors | 4,719 bytes. The average error is 9.36 and the worst is 102. |
| 2 colors | 1,208 bytes. The average error is 28.78 and the worst is 177. |
ImageMagick wrote the GIF file with 256 colors in 13,564 bytes and an average error of 2.8, and ffmpeg wrote 15,267 bytes with 255 colors. Pillow gave the same size and error with and without dithering, so we make no claim about dithering.
Saving the GIF file as a PNG changed nothing. All five decoders we tried, Pillow, ImageMagick, ffmpeg, OpenCV and Chromium, returned identical pixels for the 256 color file, and the PNG round trip was exact. Loss happens once, when colors are cut to the table.
Keep the originals. A conversion reads the GIF file and never changes it, and our hashes matched. Keep the original beside the new file. Do not upload an unknown GIF file to an online converter, because the upload shares it.
How Do GIF Files Differ From PNG, APNG, WebP and MP4 Files? The first bytes name the type
The first bytes name the type. GIF files start with GIF. PNG and APNG files start with 89 50 4e 47, and WebP files start with RIFF. MP4 files have ftyp at byte 4. PNG and APNG files share the same 8 bytes.
Five types get mixed up with GIF files because they all show pictures that move or appear on web pages. The table lists what separates them. Every row ends with its source.
| Format | What separates it |
|---|---|
| GIF file |
47 49 46 38 at byte 0, then 37 or 39 and 61. 146 of our 149 files start this way. It stores indexed colors and frames in blocks. Source: Our tests.
|
| PNG file |
Starts with 89 50 4e 47 0d 0a 1a 0a. It stores one picture with full color. Our PNG overview covers the format. Source: Our tests.
|
| APNG file | Starts with the same 8 bytes as a PNG file. ffprobe names it apng and the file command names it PNG. ffmpeg wrote one for all 50 animated GIF files. Source: Our tests. |
| WebP file | Starts with the letters RIFF, 4 size bytes and the letters WEBP. Our WebP overview covers the format. Source: Our tests. |
| MP4 file |
Has the letters ftyp at byte 4, as in 00 00 00 20 66 74 79 70. Our MP4 overview covers the format. Source: Our tests.
|
Separate them with our script
Our script reads the first 6 bytes and refuses any file that does not start with GIF. For PNG files it says the first 6 bytes are 89 50 4e 47 0d 0a. It never guesses a type, and the file command names the type it finds.
Why the ending misleads
Those six files with the ending .gif showed their true types to the file command, MediaInfo and ffprobe. An ending is a label, and the first bytes say what a file is.
Which one do you have
What Is the GIF File Format Made Of? The facts we could check, and where they came from
GIF files use a block format that CompuServe published in 1987 and extended in 1989. The facts below come from Wikipedia, PRONOM, the Library of Congress, IANA, FileInfo and our checks of 149 GIF files and 39 damaged copies.
| Part | What we could check |
|---|---|
| Maker | A CompuServe team led by Steve Wilhite. Wikipedia gives 15 June 1987 for version 87a. The GIF89a specification names July 1989, and PRONOM lists the 89a record as 31 July 1990. |
| Name | Graphics Interchange Format, or GIF. The Library of Congress calls its record Graphics Interchange Format, Version 89a, and FileInfo files it under raster image files. |
| First bytes |
47 49 46 38 37 61 (GIF87a) or 47 49 46 38 39 61 (GIF89a) at byte 0. The Library of Congress also lists the trailer as 00 3b.
|
| Blocks |
2c opens an image, 21 opens an extension and 3b ends the file. Extension labels are f9 for graphic control, fe for comment, ff for application and 01 for plain text.
|
| Colors | Up to 256 for one frame, chosen from 24 bit color. A color table has 2 to the power of n entries for n from 1 to 8, per Wikipedia. |
| Compression | LZW with codes up to 12 bits, so the highest code is 4095. Data is stored in sub blocks of 1 to 255 bytes. A length of 0 ends the data. |
| Animation | Delays are in hundredths of a second. Looping comes from the NETSCAPE2.0 application block, and a repeat count of 0 means forever, per Wikipedia. |
| Transparency | One transparent index per frame, set in the graphic control block. Disposal methods 0 to 3 are defined and 4 to 7 are reserved, and one of our files uses 7. |
| Endings | .gif. |
| Media type |
image/gif. The IANA image registry lists it with RFC 2045 and RFC 2046.
|
| Patents | Unisys owned a US patent on LZW. Wikipedia reports it expired on 20 June 2003, and the last counterparts ended in July 2004. |
| Our tests | 149 GIF files, 39 damaged copies, 10 renamed copies, eight readers, four decoders and a 69 line script on Linux. |
About the values we list
Facts about structure come from Wikipedia, the GIF89a specification and our GIF files, and every row names its source. We read the specification on the W3C site, which the Library of Congress record points to, and we make no claim that rests on it alone.
Sources differ on the date of version 89a, and we print both. The media type is a registry fact. We list no price, because the tools on this page change their terms.
What we left out
We displayed no GIF file, and we tested no browser, viewer or phone. We made no GIF file with Photoshop, and we scanned no file for malware. Our largest file is 10,130,100 bytes, so we say nothing about GIF files in the gigabytes.
Our 149 GIF files come from 5 public test folders and many are odd on purpose. Our damaged copies change one thing at a time in one file. A reader that fails on our files can still work on yours.
Sources checked
For the format, see Wikipedia on GIF files, PRONOM for GIF89a and PRONOM for GIF87a. For preservation notes, see the Library of Congress record.
For media types, see the IANA registry of image media types. For endings and programs, see the FileInfo page on GIF files.
Every count came from the files and scripts in our research folder.
Frequently Asked Questions About GIF Files
What is a GIF file?
A GIF file is a Graphics Interchange Format image. It starts with GIF87a or GIF89a, and a chain of blocks stores the colors, the frames and the timing. One frame uses at most 256 colors.
How do I open a GIF file?
Use any image viewer, like Microsoft Photos, Apple Preview or GIMP, or a tool like ffprobe, MediaInfo or gifsicle. We read all 149 of our GIF files with eight readers, and none of them needs special software.
What does GIF stand for?
GIF stands for Graphics Interchange Format. A CompuServe team led by Steve Wilhite made it, and Wikipedia gives 15 June 1987 for version 87a. The letters GIF are also the first three bytes of every GIF file.
Is a GIF file safe to open?
Reading one with an image tool is safe, because the file is data. A small file can claim a huge canvas, and the four decoders we used refused it. We displayed none of our 149 files and scanned none for malware, so check the source of any file you do not know.
What is inside a GIF file?
A 6 byte header, a 7 byte screen descriptor, an optional color table and then blocks until a trailer of 3b. Our example, testsrc.gif, is 47,768 bytes long with 20 frames. Its first image starts at byte 808.
How do I convert a GIF file to MP4?
Use ffmpeg with libx264, a pad filter and the pixel format yuv420p. That worked for all 50 of our animated GIF files, and every MP4 had the same frame count. The MP4 was smaller for 14 of the 50, so check the size.
Why will my GIF file not open?
Eight causes cover most cases. They include damaged headers, cut downloads, broken pixel data, strict readers and files that are not GIF files. Read the first bytes first, then the blocks, then count the frames.
What is the difference between GIF87a and GIF89a?
GIF89a adds the graphic control, comment, application and plain text blocks. Wikipedia lists animation delays and transparency among the additions. 131 of our 146 files say 89a, and 5 of the 15 that say 87a still use blocks that only 89a defines.
What is the difference between GIF and APNG?
An APNG file starts with the same 8 bytes as a PNG file and stores full color frames. A GIF file starts with GIF and stores indexed colors. The ffmpeg tool wrote an APNG for all 50 of our animated GIF files. The APNG was smaller for 12 of 50.
How can I check if a GIF file is damaged?
Read the first 6 bytes, then the blocks, then count the frames. Eight readers split on 22 of our 39 damaged copies. Seven copies came back exact in all four decoders, so a decoded frame is the better test.
Can a renamed GIF file still be a picture?
Yes. We copied one GIF file to 10 names, like base.jpg and base.txt, and all eight readers read all 10 as the same file. The ending is a label, and the first bytes decide what the file is.
How many colors can a GIF file have?
One frame can use up to 256 colors, chosen from 24 bit color, per Wikipedia. A color table has 2, 4, 8, 16, 32, 64, 128 or 256 entries. 49 of our global tables have 2 colors and 39 have 256, and 30 files add a local table.
Still Stuck With a GIF File? Seven routes out, depending on what went wrong
Read the first bytes and count the frames. The cause table lists what to try in order.
Convert it to MP4, APNG or WebP. The convert part shows the commands.
Is it a PNG file? The page on PNG files covers the format for a single picture with full color.
Is it a WebP file? The page on WebP files covers the format that can store both pictures and animations.
Is it an MP4 video? The page on MP4 files covers the container that GIF files convert into.
Is it a renamed program? The page on EXE files covers Windows programs, which a name like base.gif.exe can imitate.
Is it a file type you do not know? Browse the file extension resource library, or send it on the submit a file type page.