GIF Compression Explained

This may not be an exact representation of what goes on in a GIF, but I'm guessing it's close enough for our purposes.

Let's suppose you have a 6x6 gif image made up of 3 colors a,b and c...

Or in terms of a, b and c...

c c c c c c
c b b b b b         a = red
c b b b b b         b = green
c a a b b b         c = blue
c a a b b b
c c c c c c

One way to "describe" this image is

first row c-c-c-c-c-c
second row c-b-b-b-b-b

Each pixel is defined one at a time.

Let's suppose we tried to save a little time though and described it like this...


By attempting to group pixels together you save room. If we had a very large image that started off like this...

 first row c-c-c-c-c-c-c-c-c-c-c-c-c-c-c-c-c-c-c-c-c-c-c-c-c
second row c-c-c-c-c-c-c-c-c-c-c-c-c-c-c-c-c-c-c-c-c-c-c-c-c

A gif might simply say...


This is gif "compression".

Let's suppose that instead of all c's in those first two rows, there are other colors...

 first row c-c-c-c-b-b-b-a-b-b-a-a-a-a-a-c-c-b-b-b-a-b-c-c-c
second row a-a-a-a-b-b-b-a-b-c-c-c-c-c-b-b-b-b-c-c-c-c-c-c-c

This would compress into...


Now let's suppose instead of 3 colors, we reduced the number of colors to 2. We'll remove a and substitute b instead. If a and b are similar colors, the image quality may not suffer very much at all...

 first row c-c-c-c-b-b-b-b-b-b-b-b-b-b-b-c-c-b-b-b-b-b-c-c-c
second row b-b-b-b-b-b-b-b-b-c-c-c-c-c-b-b-b-b-c-c-c-c-c-c-c

This would compress into...


Compare this to the last example. Do you see how this would translate into a smaller file size?

One of the worst things you can do to a gif is make it speckled...

first row c-b-c-b-c-b-c-b-c-b-c-b-c-b

Because what would that compress into?


You don't save a thing.

So, the whole business of gif compression revolves around one simple idea... reduce the number of colors and/or color changes in the file.

Related articles you may find interesting... GIF or JPG and Image Compression Examples.