Technical7 min read•September 2026

How JPG Compression Works (Under the Hood)

Explore the 5 stages of JPG compression: color conversion, chroma subsampling, 8x8 DCT, quantization matrices, and Huffman entropy encoding.

Recommended Tool

Compress JPG

Make your JPG photos smaller so they upload faster, fit email limits, or meet a form's file-size rule. Everything runs in your browser, so your photos are not uploaded to a server.

Use Tool Free

The 5-Stage JPG Compression Pipeline

JPG achieves its remarkable compression ratios (often exceeding 10:1 or 20:1) through a sophisticated 5-step mathematical process:

1Color Space Transformation (RGB to YCbCr):

The encoder converts standard RGB pixels into Luminance ($Y$) and two Chrominance channels ($Cb$ and $Cr$). This isolates light intensity from color hue.

2Chroma Subsampling (4:2:2 or 4:2:0):

Since human vision is far less sensitive to spatial color details than luminance variations, the encoder downsamples the color channels. In 4:2:0 subsampling, 75% of chromatic data is discarded before compression even begins with zero perceptible quality drop.

38×8 Block Discrete Cosine Transform (DCT):

The image is subdivided into non-overlapping $8 \times 8$ pixel grids (64 pixels each). The Discrete Cosine Transform converts spatial pixel values into spatial frequency domain coefficients. The top-left value ($DC$ coefficient) represents average block brightness, while surrounding $AC$ coefficients capture high-frequency fine details and sharp edges.

4Quantization Matrix (The Only Lossy Step):

Each DCT coefficient is divided by a value from a predefined Quantization Table and rounded to the nearest integer. High-frequency coefficients (fine noise and microscopic textures) frequently round to zero. The "Quality" slider (0–100%) controls this quantization scale.

5Entropy Encoding (Run-Length & Huffman Coding):

The quantized 8x8 matrix is traversed in a zigzag pattern, clustering trailing zeros together. Run-Length Encoding (RLE) and Huffman statistical coding losslessly pack the numbers into a compact binary stream.