PNG vs. GIF compression

For some reason there's a widespread misconception that the GIF image format compresses images more than the PNG format.

One probable reason for this is that people make comparisons in the wrong way. Typically these people will open eg. a JPEG file in an image editor and then save it without any conversion to a PNG and a GIF and compare the file sizes directly and make the deduction that GIF compresses better.

Of course they are comparing apples to oranges here. The PNG is storing losslessly all the information in the input JPEG (that is, 24 bits per pixel) while the GIF is only storing 256 colors from the original JPEG (that is, 8 bits per pixel). In other words, the GIF is lossy (with respect to the original JPEG). No wonder the PNG file is larger, as it's storing three times as much information as the GIF.

A correct comparison would be, of course, to convert the image to 8-bit format first and then save it in PNG and GIF formats (or, alternatively, save it in GIF format, then open that GIF and save it to a PNG). This way both the GIF and the PNG contain the exact same pixel data. When the correct comparison is done, PNG beats GIF hands down in compression.


Here's a table of comparisons (click on the images to see the originals). The TGA versions are in raw format, ie. no compression at all. The size of the TGA file is always the amount of pixels (ie. width times height) plus 768 bytes for the palette plus 18 bytes for the TGA header (so for example a 800x600 image always takes exactly 480 786 bytes). This is used as a comparison.

ImageFormatSize (bytes)% of TGA
TGA 480 786
GIF 331 768 69%
PNG 261 530 54%
TGA 480 786
GIF 490 479 102%
PNG 387 102 81%
TGA 1 883 879
GIF 29 611 1.6%
PNG 14 595 0.8%
TGA 417 408
GIF 88 640 21%
PNG 75 011 18%
TGA 387 186
GIF 249 060 64%
PNG 205 152 53%
TGA 909 074
GIF 769 297 85%
PNG 439 036 48%
TGA 400 555
GIF 17 613 4.4%
PNG 1 873 0.5%

PNG beats GIF in all the testcases. Some images, such as the second one (the blue fractal image) are in fact so pathological that the GIF file is actually larger than the raw TGA file.

For some reason it seems that GIF compresses grayscale images very poorly compared to PNG. While PNG has directly a grayscale mode (ie. it doesn't use a palette) while a GIF always uses a palette, the palette data in the GIF file only takes 768 bytes so that doesn't explain the poor result.

Another prominent example is the simple vertical gradient image which compresses as PNG to almost one tenth of the size of the GIF.