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.
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.
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:
The encoder converts standard RGB pixels into Luminance ($Y$) and two Chrominance channels ($Cb$ and $Cr$). This isolates light intensity from color hue.
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.
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.
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.
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.