How GIF Palettes, Dithering, and LZW Compression Work
Why GIF is limited to 256 colors, how median-cut quantization chooses them, what Floyd–Steinberg dithering does, and how LZW shrinks the result.
Primary Browser Tool
GIF Maker
Build an animated GIF from a sequence of images with frame delay, loop, size, and frame order controls.
Indexed Color: Every Pixel Is a Table Lookup
GIF dates from 1987 and stores images in indexed color. The file contains a table of up to 256 colors, and each pixel is a single byte giving a position in that table. A photograph can contain hundreds of thousands of distinct colors, so making a GIF means choosing 256 representatives and assigning every pixel to one of them. This step is called quantization, and it determines most of how a GIF looks.
Choosing the 256 Colors: Median Cut
The encoder gathers the colors used across all frames and treats them as points in a three-dimensional red-green-blue space. Median cut begins with one box containing every point. It repeatedly picks the box most worth splitting, considering both how wide its color range is and how many pixels it holds, and divides it along its longest axis at the point where half the pixels fall on each side. After 255 splits there are 256 boxes, and the average color of each becomes a palette entry.
The effect is that the palette spends its entries where the image’s colors actually are. A sequence dominated by skin tones gets many subtly different browns and pinks and few blues. RapidPNG builds one palette for the whole animation, which keeps colors stable from frame to frame and avoids flicker.
Dithering: Trading Banding for Noise
Mapping each pixel to its nearest palette color leaves smooth gradients as visible bands. Floyd–Steinberg dithering reduces this. After each pixel is assigned, the difference between its true color and the palette color is passed on to the neighboring pixels that have not been processed yet: seven sixteenths to the right, and three, five, and one sixteenths to the three pixels below. Those neighbors are nudged so the average color over a small area is correct, even though every individual pixel uses a palette color.
| Content | Dithering | Reason |
|---|---|---|
| Photographs, gradients | On | Hides banding in skies and skin |
| Screenshots, UI | Off | Few colors already; dithering adds speckle to flat areas |
| Flat illustrations, logos | Off | Keeps solid fills clean and files small |
| Pixel art | Off | Preserves exact colors |
LZW: Compressing Repetition
Each frame’s stream of palette indices is compressed with LZW, a lossless method that builds a dictionary of sequences as it reads. The first time a run of indices appears it is stored in full; when the same run recurs it is replaced by a single short code. Codes start small and grow up to 12 bits, at which point the dictionary is reset.
LZW rewards repetition. Flat areas and repeated patterns compress extremely well, while noise does not. This is the reason dithering increases file size: it replaces long runs of one index with an irregular mixture that the dictionary cannot predict.
Practical Examples
The same 20 frames, two settings
A photographic sequence at 480 px is 3.1 MB with dithering and 2.2 MB without, but the undithered version shows clear banding in the sky. A screenshot sequence is 310 KB without dithering and 520 KB with it, with no visual benefit.
Common Mistakes & Honest Limitations
Mistakes to Avoid
- Assuming GIF compression is lossy like JPEG. The loss happens in the reduction to 256 colors; LZW itself is lossless.
Technical Limitations
- Frames are stored in full. The encoder does not store only the regions that changed between frames, which some specialized optimizers do to reduce size further.