← Back to Home

How JPEG Compression Works

The stages inside a JPEG encoder, from colour conversion to entropy coding.

By the CompressItAll team. Published 2025-11-16. Updated 2026-10-24.

Compress JPEG images with our free tool

Quick Summary

  • JPEG is a lossy format built for photographs and other continuous-tone images.
  • The encoder converts colour, optionally shrinks the colour channels, transforms 8x8 blocks into frequencies, rounds those frequencies, and then packs the result losslessly.
  • The only step that deliberately discards visual information at scale is quantisation, plus chroma subsampling when it is used.
  • The quality slider mostly controls how coarse the quantisation tables are.

This page explains what happens inside the encoder. If you want a practical export routine instead, read the JPEG Optimization Guide. If your question is what "no quality loss" really means, see Compress JPEG Without Losing Quality.

JPEG lossy compression: original file size compared to compressed output at quality 85. Placeholder for real measurement.
Placeholder — replace with real measurement from compressitall.com/compress-jpeg/ (lossy mode, quality 85)

JPEG Fundamentals

JPEG is named after the Joint Photographic Experts Group, the committee that wrote the standard. It was designed for photographs, where neighbouring pixels are usually similar and where small errors in fine detail are hard to see. Unlike PNG, which stores pixels exactly, JPEG stores an approximation. That trade is what lets a photo shrink a great deal, and it is also why JPEG is a poor fit for screenshots, logos and text.

The Pipeline, Stage by Stage

StageWhat it doesLossy?
Colour space conversionRGB to luminance plus two colour-difference channelsOnly through rounding
Chroma subsamplingStores colour channels at reduced resolutionYes, when used
Block splitting and DCTConverts each 8x8 block into frequency coefficientsOnly through rounding
QuantisationDivides coefficients by table values and roundsYes, the main step
Entropy codingRun-length and Huffman coding of the rounded valuesNo

1. Colour Space Conversion

Screens work in red, green and blue, but those three channels are strongly correlated. JPEG converts to YCbCr: Y carries brightness, while Cb and Cr carry blue-difference and red-difference colour information. The reason is human vision. People notice small changes in brightness much more readily than small changes in colour, so the two colour channels can be treated more roughly than Y without the picture looking worse.

2. Chroma Subsampling

Subsampling reduces the resolution of Cb and Cr while Y stays at full resolution, as defined in ITU T.81 (JPEG standard). The usual notation compares them:

  • 4:4:4 keeps full colour resolution. Nothing is dropped at this stage.
  • 4:2:2 halves colour resolution horizontally.
  • 4:2:0 halves it both horizontally and vertically, so each colour sample covers a 2x2 area of pixels. It is common in cameras and web encoders.

Subsampling is usually unnoticeable in photographs but can show as colour fringes around thin, saturated lines, such as red text on a blue background. That is one reason JPEG handles graphics badly.

3. Splitting Into 8x8 Blocks

Each channel is divided into 8x8 pixel blocks, and the values are shifted so they centre on zero. If the image dimensions are not multiples of the block size, the edges are padded. Blocks are processed independently, which keeps the format simple and fast, and which also explains the square patches that appear at low quality.

4. The Discrete Cosine Transform

The DCT re-expresses a block as a weighted sum of 64 fixed patterns, from a flat tone through gradually increasing horizontal and vertical stripes, as defined in ITU T.81 (JPEG standard). The first coefficient, called DC, is the block average. The other 63, called AC coefficients, describe progressively finer detail. In a photo, most of a block's energy lands in a few low-frequency coefficients, while the high-frequency ones are small. The transform itself loses nothing beyond rounding error; it just arranges the data so that the next step can discard the right parts.

5. Quantisation

Each of the 64 coefficients is divided by a value from a quantisation table and rounded to an integer. Table values are larger for high frequencies, so fine detail is rounded more coarsely and many coefficients become zero. This is where information is permanently lost. Separate tables are normally used for brightness and colour. When you pick a lower quality setting, the encoder scales the tables up, which means more rounding, more zeros and a smaller file. This is also why the quality number is not a universal measurement: encoders can start from different base tables.

6. Entropy Coding

After quantisation, the coefficients are read in a zigzag order that puts low frequencies first and groups the zeros together at the end. Runs of zeros are stored compactly, the DC value is stored as a difference from the previous block, and the result is packed with Huffman coding. The JPEG standard also allows arithmetic coding, but it is rarely used because support is limited. This final stage is lossless: decoding it returns exactly the quantised values.

Decoding and Where Artifacts Come From

A decoder reverses the steps: it decodes the entropy-coded data, multiplies by the quantisation tables, applies the inverse DCT, upsamples colour and converts back to RGB. The rounding in quantisation cannot be undone, so the decoded block is close to the original but not identical. Typical artifacts map directly onto the stages:

  • Blocking comes from coarse DC and low-frequency rounding in neighbouring 8x8 blocks that no longer match at their borders.
  • Ringing near edges comes from discarding the high-frequency terms that were needed to describe a sharp edge.
  • Colour bleeding comes from chroma subsampling and coarse chroma tables.

Baseline and Progressive Encoding

The same coefficients can be stored in two layouts. A baseline file stores each block's data in one go, so the image paints from top to bottom. A progressive file stores the data in several scans, starting with coarse information for the whole picture and adding detail in later passes. The decoded image is the same either way; only the order of arrival and the packing differ.

What This Means in Practice

  • Smooth, photographic content compresses well because its detail sits in the low frequencies.
  • Sharp edges and text need high frequencies, so they either cost many bytes or show ringing.
  • Re-saving repeats quantisation, and if the block grid shifts, the new rounding does not line up with the old, so errors accumulate.
  • Resizing before encoding reduces the number of blocks, which cuts size more reliably than lowering quality.

Those points turn into a concrete routine in the JPEG Optimization Guide. For how newer formats rethink this design, see How WebP Compression Works and How AVIF Compression Works.

When JPEG Is the Right Choice

  • Photographs and realistic images with smooth tones and fine natural texture
  • Content that must open in any software, device or email client (caniuse)
  • Pictures where a small file matters more than exact pixels

When Another Format Is Better

  • Transparency is needed. JPEG has no alpha channel.
  • The image contains text, logos, line art or screenshots. Use PNG, or see Lossy vs Lossless Compression.
  • The file will be edited and saved repeatedly.

Applying This to Your Files

If you want to compress a JPEG file now, use the how to compress a jpeg tool. In Lossy mode the quality defaults to 85, and the results table shows sizes with a before and after view so you can look for the artifacts described above. For batches, see the Batch Compression Guide.