How image compression works

A photograph goes into a JPEG encoder and comes out a tenth of the size. Here is each step of what happens in between, without the mathematics.

The pipeline at a glance

1. Colour transform
RGB is converted to one brightness channel and two colour channels.
2. Chroma subsampling
The colour channels are stored at half resolution. Lossy.
3. Block split
The image is divided into 8 × 8 blocks.
4. Frequency transform
Each block becomes a set of wave patterns (the DCT).
5. Quantization
Fine patterns are divided down and rounded, often to zero. Lossy — this is the step.
6. Entropy coding
The result is packed losslessly with Huffman or arithmetic coding.

Step by step

Step 1 — separate brightness from colour

The encoder first converts red, green and blue into a different set of three numbers: one for luma (brightness) and two for chroma (colour).

This is not compression by itself — it is preparation. Human vision resolves brightness far more finely than colour, so once the two are separated the encoder can treat them differently, and spend its bytes where they will be noticed.

Step 2 — halve the colour resolution

With colour on its own channels, the encoder can simply store fewer of them. In the standard 4:2:0 arrangement, each colour sample covers a 2 × 2 block of pixels — a quarter of the colour data for no visible cost on a photograph.

This is the first genuinely lossy step, and it is nearly free. The failure case is worth knowing: saturated coloured text or a thin red line will smear and fringe, because there the colour detail is the content.

Step 3 — describe each block as waves

The image is cut into 8 × 8 blocks, and each block is transformed into a set of coefficients describing how much of each wave pattern it contains — from a flat block of uniform colour, through gentle gradients, up to fine alternating checkerboards.

This is the discrete cosine transform, and it is completely reversible. Nothing has been lost yet. What it achieves is a reordering of the information: after the transform, the handful of coefficients describing coarse patterns carry nearly all the visual weight, and the many describing fine detail carry very little.

Step 4 — quantization, where the loss happens

Each coefficient is divided by a value from a quantisation table and rounded to a whole number. Coarse patterns are divided by small numbers and survive. Fine patterns are divided by large numbers and round to zero.

That rounding is irreversible, and it is the entire lossy component of JPEG. Zeros cost almost nothing to store, so the more coefficients round to zero, the smaller the file.

The quality setting you choose does exactly one thing: it scales this table. Quality 90 divides gently and keeps most detail; quality 40 divides hard and discards most of it. The number is not a percentage of anything — it is an index into how aggressively this division is done.

Step 5 — pack it losslessly

What remains is a sparse list of small numbers with long runs of zeros. The encoder reads each block in a zig-zag order, which groups the zeros together, then applies entropy coding — Huffman coding in classic JPEG, arithmetic coding in newer formats — which gives short codes to common values and long codes to rare ones.

This final step is completely lossless. It contributes a substantial part of the size reduction and costs nothing in quality.

Why newer formats do better

WebP and AVIF keep the same broad shape but replace the weakest part. Instead of describing every block independently, they predict each block from the pixels already decoded above and to its left, and encode only the difference between prediction and reality.

Because most of an image is highly predictable from its neighbours, those differences are small, and small numbers cost few bits. That single change is most of why WebP is roughly 25–35% smaller than JPEG at matched quality, and why AVIF goes further still.

Try it yourself

Everything below runs in your browser, so you can test any of this on your own files without uploading them.

  • Compress JPEG

    Shrink JPG and JPEG photographs with a quality slider or an exact target size.

    Open the tool
  • Compress WebP

    Re-encode existing WebP files, or convert JPEG and PNG into WebP.

    Open the tool
  • JPG to WebP

    Typically 25–35% smaller at matched quality. The standard web win.

    Open the tool

Questions

Frequently asked questions

Every answer below is present in the page source, expanded, so it can be read without opening anything.

Which step actually loses the quality?

Two of them. Chroma subsampling discards three quarters of the colour resolution, which is invisible on photographs and damaging on coloured text. Quantization rounds the frequency coefficients, and that is the step the quality slider controls — it is where nearly all of the size reduction and all of the visible artefacts come from.

Why 8 × 8 blocks?

It was a practical compromise when JPEG was standardised in 1992: large enough to find real redundancy, small enough that the transform was affordable on the hardware of the day. It is also why JPEG artefacts appear as visible squares — the blocks are processed independently, so at low quality their edges stop matching.

Does the quality number mean the same thing in every tool?

No. The quantization tables are not standardised, so quality 80 in one encoder can produce a different file from quality 80 in another. The ranges are broadly comparable, but do not expect byte-identical results from different software at the same setting.

Is any of this reversible?

The colour transform, the DCT and the entropy coding are all reversible. Chroma subsampling and quantization are not. Since those two are where the compression happens, the process as a whole is one-way.

How does PNG differ from all this?

Completely. PNG has no transform and no quantization step. It filters each row of pixels into differences from their neighbours, then runs DEFLATE — the same algorithm a ZIP file uses — over the result. Nothing is discarded, which is why it is lossless and why it is poor at photographs.

Where does metadata fit in?

Outside all of it. EXIF, colour profiles and thumbnails live in separate blocks in the file's container, untouched by the image pipeline. That is why stripping metadata is lossless and why re-encoding an image discards it by default — the new file is written from pixels, and nothing copies those blocks across.

From the blog

Related reading

Longer, more practical guides from the blog.

  • Image Compression

    JPEG Quality Settings Explained: What the Number Actually Means

    Quality 80 is not 80% of anything. It is an index into a quantisation table. Measured on one photograph: dropping from 100 to 90 removes 77% of the file and 4.2 dB of fidelity; dropping from 40 to 30 removes 17% and costs almost as much.

    8 min read

  • Image Formats

    WebP vs JPEG: Which Should You Use, and When JPEG Still Wins

    WebP produced a 27.0 KB file against MozJPEG's 41.1 KB on the same photograph at slightly higher measured fidelity — 34% smaller. It also produced a larger file than JPEG on random noise. The advantage is real and content-dependent.

    9 min read

  • Practical Guides

    How to Compress Images for a Website Without Wrecking Them

    Resize before you compress. Measured on one photograph: half the width at quality 80 produced a 16.4 KB file, while full width at quality 40 produced 20.6 KB and looked far worse. The order of operations matters more than the settings.

    8 min read