The GIF format is not as well suited to compressing pictures with semi-random patterns of dots of differing colors, and it is even less suited to compressing images which have extremely subtle gradations of colors such as one might see in a photograph. The images at the right show closeups of such patterns.
On the other hand, the JPEG compression scheme, (also called JPG), has been designed to process photographic images. The bottom closeup takes up 6,456 bytes when saved as a GIF file, but only 1,573 bytes as a JPEG.
Rather than looking for patterns or runs of colors on a line-by-line basis, JPEG uses a mathematical formula based on the way your eyes perceive color. The JPEG compression algorithm looks at a pixel and its neighbors in all directions and finds the factors for that formula which will best represent all those pixels. Rather than storing the pixels, it compresses the image by storing only the factors. rather than the pixels, to save space. When you view the pictures, the JPEG process plugs the factors back into the formula and generates the pixels that best represent the original image. The key words here are “best represent.”
Note: The explanation of the JPEG algorithm above is a massive oversimplification. If you really, really want to know more, either see the JPEG links page or the compression FAQ, but be warned that the latter reference is not for the faint of heart.