Lossy vs lossless compression

Every image compressor does one of two things: finds a shorter way to write the exact same pixels, or decides which pixels you won’t miss. Here is how each works.

Lossless: same pixels, shorter description

Lossless compression looks for predictability. PNG guesses each pixel from its neighbours (left, above, or a mix), stores only the difference from the guess, and passes those mostly-small numbers to DEFLATE, the algorithm inside zip files, which replaces repeated sequences with back-references. Decompress it and you get every pixel back exactly.

Flat graphics are very predictable, so they shrink well. Photos are full of sensor noise, which is by definition unpredictable, so lossless photos stay big. Reducing the colour count is the classic way to make graphics even more predictable:

Sample
Colour budget

0 distinct coloursPNG: 0 B

These byte counts come from your browser’s own PNG encoder, run live. PNG’s filters and DEFLATE find repeats; fewer colours mean more repeats. A flat logo barely changes when you cut its colours, while the gradient turns into visible bands. That is why this site’s PNG mode picks a palette with a quantiser and then lets OxiPNG squeeze the result.

Lossy: throw away what the eye ignores

Lossy codecs exploit human vision. We see brightness detail much better than colour detail, so JPEG usually stores colour at half resolution (chroma subsampling). And we notice broad shapes more than fine texture, so JPEG turns each 8×8 block into frequencies and rounds the fine ones hard. Move the slider and watch the frequency grid empty out:

Original 8×8 pixels

Frequencies kept 4/64

30-3-14-1

What gets decoded

Top-left cells are smooth, broad changes; bottom-right cells are fine detail. Each is divided by a step from the JPEG quantisation table and rounded, so small high-frequency values become 0, and runs of zeros are almost free to store. At quality 50, 60 of 64 values are zero and the average pixel is off by 0.9 levels (out of 255). Try the “Hard edge” block at quality 10 to see ringing, the halo you get round text in over-compressed JPEGs.

Frequency maths like this tends to land better when someone explains it aloud. For how-it-works questions far from image files, ahaboo has narrated explainers on photosynthesis and why the Moon has phases.

WebP and AVIF use the same basic idea with smarter tools: they predict each block from already-decoded neighbours, use variable block sizes, and filter block edges after decoding, which is why they avoid JPEG’s blockiness at low sizes.

Which should you use?

ImageUseTool
Photos for the webLossy (quality 70–80)JPG, WebP, AVIF
Logos, icons, UI screenshotsLossless, or palette PNGPNG
Masters you’ll edit againLosslessPNG (lossless) or keep the camera original
Upload forms with a KB limitLossy with a size targetCompress to a size

Sources: ITU-T Recommendation T.81 (the JPEG standard) for the quantisation table and DCT; the PNG specification (W3C) for filtering and DEFLATE.

Questions

Is lossy compression always worse?

No. For photos, lossy formats keep what your eye notices and discard what it doesn’t, and a well-tuned lossy file is usually indistinguishable at normal size while being many times smaller than a lossless one.

Which formats are lossless?

PNG and GIF (apart from their colour limit) are lossless. WebP, AVIF and JPEG XL have both lossless and lossy modes. Standard JPEG is always lossy.

Does converting a JPEG to PNG restore quality?

No. PNG will store the JPEG’s pixels, artefacts and all, exactly, in a much bigger file. Lost detail can’t be recovered.

What is “visually lossless”?

A lossy setting high enough that people can’t tell it from the original under normal viewing. It’s a judgement, not a guarantee, which is why the compressor has a side-by-side compare.