Skip to content
GIF Toolkit

How the GIF Format Works

By GIF Toolkit · 6 min read ·

GIF is almost forty years old and still everywhere. Understanding its few simple rules explains most of the quirks you run into, from banded colors to huge file sizes to odd playback speeds.

A short history

CompuServe introduced the Graphics Interchange Format in 1987 as a way to exchange images over slow connections. The 89a revision in 1989 added the features that made animation possible: multiple frames, per-frame delays and transparency. The format survived because it was simple, free to implement and supported by the earliest web browsers.

The color palette

A GIF is an indexed-color image. Instead of storing a full color for every pixel, it keeps a table of up to 256 colors and each pixel points to an entry in that table. This is why photographs look rough as GIFs: a photo contains thousands of distinct shades, and squeezing them into 256 forces banding.

A GIF can have one global palette shared by all frames, or a separate local palette for each frame. A converter that builds a palette from your actual clip picks the 256 most useful colors for that content, which gives far better results than a generic fixed palette.

Dithering

When the palette lacks a color, dithering fakes it by placing pixels of two or more nearby colors close together, so the eye blends them. Floyd–Steinberg dithering spreads the rounding error to neighboring pixels and looks natural but noisy. Ordered or Bayer dithering uses a fixed pattern that looks more regular and compresses a bit better. No dithering gives flat bands but the cleanest, smallest result for graphics.

LZW compression

GIF compresses its pixel data with a lossless method called LZW. It builds a dictionary of repeated pixel sequences as it reads the image, then replaces repeats with short codes. This is very effective on large flat areas and repeated patterns, and weak on noisy or finely detailed images, which is the second reason photographs make big GIFs.

Because the compression is lossless, the only quality loss in a GIF comes from the color reduction step. Once a GIF has been made, shrinking it further means reducing colors or dropping frames, both of which discard information.

Frames and delays

An animated GIF is a sequence of images, each with a delay measured in hundredths of a second. Since delays are whole hundredths, the finest control is 10 milliseconds, and a frame rate such as 30 fps (33.3 ms per frame) has to be rounded to 30 or 40 ms. Browsers also tend to treat very small delays as slower than requested, so extremely fast GIFs often play more slowly than intended.

The format can also store only the part of a frame that changed from the previous one, which saves space for animations with a static background. Many converters do this automatically.

Transparency and looping

A GIF can mark exactly one palette color as transparent. Pixels are either fully visible or fully invisible, with no partial opacity, so soft edges and shadows cannot blend smoothly against a background. Looping is controlled by an extension block that holds a repeat count, with zero meaning forever.

What this means in practice

GIF is a good fit for flat graphics, simple animation, screen recordings and short, small loops. It is a poor fit for long, high-resolution or photographic content. For those, a video format such as MP4 is dramatically more efficient, as our comparison of GIF, MP4 and WebP explains.

Keep reading