A Few Thoughts on Image Compression
By
Multispeed
There are two schools of thought in this area and I’m not saying which is better or advocating which one is best! In fact, I think as Adult Webmasters, we should be in the middle on this so that our sites are cleaner and faster!
Here are the common thoughts:
There are 2 widely used image file formats on the web - GIFs and JPEGs. (There is also a new format called "PNG", and though it's a very good one, it isn't yet fully supported, so it's rarely used today and isn't practical at the moment). Each has its own strengths and weaknesses, and the first choice you make when optimizing an image is choosing one of these formats.
Here is a brief overview of GIFs and JPEGs:
GIFs
The GIF file format is a very simple format which defines an image to the web browser pixel-by-pixel. There are 2 methods in which the GIF uses to compress an image - first, it reduces the amount of colors in the image to 256 or less: this means that the less colors the image had to begin with, the better the GIF will turn out. The second method is that it saves large lower numbered color spaces in a compressed manner (without going into detail, basically this is done by saving a line of pixels as one file-cell, instead of many - so instead of showing 5 pixel in a horizontal row as "36, 36, 36, 36, 36", it will show them as ("36[5]").
This can be taken advantage of by filling in large spaces with a single color or high contrast low number of colors images and still having a small file size. The types of images that would turn out well as a GIF are ones that don't have many colors to begin with, for instance - cartoons, line art, flat text, and large fill areas.
Also, any image with contrasting shades (for instance, black-and-white) would look perfectly as a GIF, because you don't need more than the 256 colors, that a GIF allows in order to define shades. Actually, if the image only has one shade or high contrast, you should consider saving it using less colors, since a high contrast color or "black-and-white" image looks fine, and would take much less time to load.
JPEG (or "JPG")
The JPEG file format compresses an image by generalizing it.
It averages out areas of the image, and gives you the "summary" of it. The JPEG format supports 32Bit color depths, so it's very useful for saving photos or images with many colors and shades.
While with a GIF you can control a file's size by reducing/increasing the image's color depth (but only up to 256), with JPEGs you choose the level of compression when saving the file. The more compressed, the less detail it will have. The level of compression ranges from 1 to 100.
It's a good exercise to take an image, save a copy of it (so you don't damage the source), and then save the copy in as many JPEGs with different levels of compression. That will give you a "feeling" of how JPEG compresses an image and what to expect from each level of compression (of course, the more images you try this on, the better you'll understand it).
One of the weaknesses of a JPEG is that it normally won't "see" the exact color when it compresses the image, so if you save an image with a certain background as a JPEG, and then enter that image into an HTML with that background, there's a high probability that the backgrounds won't match exactly.
The types of images that would turn out well as JPEGs are photos, general high-color images, and textures. The types of images that wouldn't turn out well as JPEGs are those that would most likely look good as GIFs -line art, flat texts, small details, and sharp contrast fills.
Dithering" and Banding
Two terms you'll need to understand before we go on are "Dithering" and "Banding".




