Gif compression algorithm

Gif compression algorithm cover image
  1. Home
  2. Graphics
  3. Gif compression algorithm

The Gif file format uses the LZW compression algorithm developed by Abraham Lempel, Jakob Ziv and Terry Welch. The compression algorithm constructs a color table for an image wherein each color value is matched to a pixel. Thus, images with large areas of one color will be compressed far better than those that those that do not have such color blocks.

It, therefore, follows that simple images and graphics like line art and logos are better saved as Gifs as they are likely to contain areas of single flat color and hence will be compressed more. Photographs on the other hand are much more complex images and the LZW algorithm cannot compress these very well.

Sponsored Links

Let us take a look at two example. Note the file size and clarity (or quality, if you like) of the images below.

Image compressed by LZW compression algorithmSimple image that contains areas of flat colors is compressed well by LZW algorithm: Only 1,110 bytes

Complex images cannot be compressed well by LZW gif compression algorithmComplex images are those without areas of flat color and generally contain many colors. The LZW Gif compression algorithm cannot reduce the file sizes of such images well: 17,848 bytes

Horizontal Pixel change

One more point on the LZW compression algorithm – it counts the pixel change horizontally. Therefore, images that involve horizontal color changes will be larger than those that have vertical color changes. Take a look at the two example images below:

Gif image compressed using the LZW compression algorithm - Little horizontal color changeAn image with little horizontal color change: 324 bytes

Gif image with a significant amount of horizontal color change compressed by the LZW compression algorithmAn image with a significant amount of horizontal color change (It is the two differently colored vertical bands that cause so much of horizontal color change): 1109 bytes

And since we are on this point, what if, instead of 2 colors (as in the images above), there were several colors? Let us compare the file sizes:

Gif with 6 different color bands - lesser horizontal color change626 bytes

Gif with 6 vertical color bands that cause a lot of horizontal color change 1286 bytes

You’ll notice that the image that had lesser horizontal color change (the one with horizontal color bands), almost doubled in file size while the one in which we had vertical color bands increased only about 15%.

Finally, the GIF compression algorithm is lossless, which means that no information is lost from the original image when its converted to a gif. Since we know that a Gif image can have only 256 colors, if your original image contained more than 256 colors, then some information will be lost. However, once converted to a gif there will not be any further loss.

We’ll discuss these points in detail along with examples in sessions on optimizing GIFs.

Graphics