How JPEG makes images small

· 2 min read

#how-it-works#data

JPEG is the image format behind most photos on the web, and it makes files small by giving up some detail. The idea to keep: the quality setting decides how much detail is given up.

The short version

JPEG is lossy, so the decoded pixels may differ from the original. The encoder first turns RGB into a brightness channel and color channels, then cuts the picture into 8x8 blocks and runs a DCT on each block. The quality setting scales the quantization tables, and in this simplified view that rounding step is where detail is lost. What remains is read in zigzag order and Huffman-coded.

JPEG encoder pipeline Five steps from left to right: RGB pixels, luma and chroma, 8x8 blocks with a DCT, quantize, then zigzag and Huffman coding. The quality setting scales the quantize step. JPEG encoder pipeline (simplified) RGB pixels Luma + chroma 8x8 blocks + DCT Quantize (lossy) Zigzag + Huffman 1 2 3 4 quality setting scales the tables
Simplified: a quality setting between 1 and 99 scales the tables used in the quantize step.

Step by step

  1. Convert the RGB pixels into luma (brightness) and chroma (color).
  2. Optionally shrink the color channels: in the 4:2:0 layout they are halved horizontally and vertically.
  3. Split the picture into 8x8 blocks and apply a DCT to each one.
  4. Quantize the results with tables scaled by the quality setting.
  5. Read the values in zigzag order and Huffman-code them.
Lower quality Higher quality
File size Smaller Larger
Visible artifacts More likely Less likely

Why it matters

JPEG suits photos and smooth color transitions, so most camera pictures use it, and typical settings shrink a file by roughly 10:1 to 20:1. The downside is lost detail, and sharp lines and sudden edges compress badly and need a high quality setting to avoid visible artifacts. MDN calls lossy compression unsatisfying for diagrams and graphics.

Sources