◩ Graphics & images

Image compression: lossless and lossy approaches compared

How image file size is reduced: redundancy, RLE, Huffman coding, prediction, lossy compression and why some images compress so poorly whatever you try.

An uncompressed 12-megapixel photo takes about 36 MB; the same shot as a JPEG takes only a few. A screenshot of a text window saved as PNG often weighs less than a JPEG photo of the same size. To understand why, and to choose the right format without guesswork, it pays to know the principles shared by all image compression algorithms.

Two families

  • Lossless compression: after decompression you get exactly the same pixels you started with, bit for bit. Examples: PNG, GIF, WebP's lossless mode, TIFF with LZW or ZIP compression.
  • Lossy compression: information judged barely visible is discarded, producing much smaller files. The decompressed image is similar, not identical. Examples: JPEG, and WebP and AVIF in their lossy modes.

The choice is not about "quality" in the abstract, but about the type of content and how it will be used.

The principle of lossless compression: redundancy

You can compress without losing anything only if the data contains redundancy, that is, predictable regularities. An image of random noise does not compress at all; an image with large uniform areas compresses enormously. The main techniques exploit different kinds of redundancy.

Run-length encoding (RLE)

Runs of identical values are replaced by the pair "how many times, which value." A row of 1000 white pixels becomes "1000 × white." It works well for simple graphics and logos and is useless for photographs, where two identical consecutive pixels are rare.

Dictionaries (the LZ family)

Lempel–Ziv family algorithms look for sequences already seen and replace them with a backward reference: "repeat the 20 bytes located 300 positions earlier." They work well with repeated patterns, such as icons in an interface or a decorative pattern.

Entropy coding

Not all values occur with the same frequency. Huffman coding gives short codes to frequent values and long codes to rare ones, just as in Morse code the letter E has the shortest signal. Arithmetic coding and its variants do the same more efficiently. The theoretical limit is the entropy of the data: no lossless method can go below that threshold on average.

Prediction

Images have strong spatial correlation: a pixel resembles its neighbours. Instead of storing the value, you store the difference between the actual value and a prediction based on pixels already known. In a uniform sky the differences are almost all close to zero, and a sequence of small numbers compresses far better than the original values. This is the core of PNG's filters and of many modern lossless formats.

The principle of lossy compression: what you can't see

Lossy compression exploits the limits of vision:

  1. We are less sensitive to color than to brightness: the resolution of color information can be reduced (chroma subsampling) with modest visual impact.
  2. We are less sensitive to fine detail than to broad variations: the image is transformed into frequency components (with the DCT in JPEG, with similar transforms in newer formats) and high frequencies are represented with less precision.
  3. Noise and grain need not be reproduced exactly: some modern codecs remove them and recreate them synthetically on decoding.

The stage that actually throws information away is called quantisation: values are rounded to a limited number of levels. After quantisation, the rest of the chain is lossless again: prediction and entropy coding compress the remaining data.

Which content for which compression

Content Suitable compression Reason
Photographs lossy noise and continuous gradients compress poorly losslessly
Screenshots, interfaces lossless sharp edges and flat areas; lossy creates artifacts around text
Logos, icons, flat graphics lossless or vector few colors, lots of redundancy
Working masters lossless every lossy save degrades further
Scientific and forensic images lossless pixel values are data, not just appearance

Why some images "won't compress"

A few examples I often come across in my work:

  • High-ISO photos: sensor noise is, in effect, randomness, and randomness has no redundancy. Light noise reduction before compression cuts file size far more than you would expect.
  • Photos saved as PNG: a PNG of a photograph is often three or four times heavier than a good-quality JPEG, with no visible benefit for web use.
  • Dithered graphics: the noise added to remove banding increases file size.
  • Repeated recompression: opening a JPEG, editing it and saving it again lossily accumulates artifacts at each pass. The size does not drop; the quality does.

A golden rule

Always keep a lossless original, or the RAW file, and generate the compressed versions from it. Lossy compression is a delivery format, not a working one.

In short

  • Lossless compression exploits redundancy: RLE, dictionaries, prediction and entropy coding.
  • Lossy compression exploits the limits of vision and discards information through quantisation.
  • Photographs: lossy; screenshots, logos and masters: lossless.
  • Noise is the enemy of compression: it compresses poorly in any format.
  • Work on lossless originals and compress lossily only on export.
MM
Marco Marzaduri

Marco Marzaduri works in cybersecurity, digital forensics and web development. A computer science graduate, he combines rigorous technical analysis with attention to the people who use technology.

Read also