188.8.131.52 How can I avoid artifacts and still get good JPEG compression?
(Answer by Peter J. Holzer)
First, you have to know a little bit about how a picture is stored in JPEG format.
Unlike most image formats it does not store RGB values, but YUV values (1 grayscale value and two "color
difference" values) just like they are used in a color TV signal. Since the human eye uses mostly the gray values
to detect edges, one can usually get away with storing the color information at a lower resolution - an 800x600 JPEG
typically has only grayscale information at 800x600, but color information at 400x300. This is called supersampling.
For each color channel separately, the picture is then divided into little squares and the cosine transform of each
square is computed. A neat feature of this transformation is that if you throw away only a few of the values, the
quality will degrade very little, but the image will compress a lot better. The percentage of values stored is called
Finally, the data is compressed.
Most programs only let you change the quality setting. This is fine for photos and photorealistic renderings of
"natural" scenes. Generally, quality values around 75% give be best compromise between quality and image
However, for images which contain very saturated colors, the lower resolution of the color channels causes visible
artifacts which are very similar to those caused by low quality settings. They can be minimized by setting an
extremely high quality (close to 100%), but this will dramatically increase the file size, and often the artifacts are
A better method is to turn off supersampling. The higher resolution will cause only a modest increase in file size,
which is more than offset by the ability to use a lower quality setting.
The cjpeg command line utility (which should be available for all systems which have a command line, e.g., Linux,
MS-DOS, Unix, ...) has an "-sample" to set the sampling factors for all passes.
cjpeg -sample 1x1,1x1,1x1 -quality 75 -optimize
should be good default values which have to be changed only rarely.