How JPEG makes images small
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.
Step by step
- Convert the RGB pixels into luma (brightness) and chroma (color).
- Optionally shrink the color channels: in the 4:2:0 layout they are halved horizontally and vertically.
- Split the picture into 8x8 blocks and apply a DCT to each one.
- Quantize the results with tables scaled by the quality setting.
- 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
- Library of Congress, JPEG format description: lossy modes use a DCT on 8x8 blocks, RGB is converted to YCrCb, baseline coefficients go in zigzag order, typical settings shrink files about 10:1 to 20:1
- RFC 2435, RTP payload format for JPEG: quantization tables are the standard tables scaled by a quality value from 1 to 99, standard Huffman tables are used, 4:2:0 halves the chroma channels in both directions
- libjpeg-turbo README.ijg: JPEG is normally lossy, suits photographs and smooth transitions, trades quality for size, and handles sharp lines and sudden edges badly
- MDN, Image file type and format guide: JPEG is lossy, DCT-based and meant for photos, and lossy compression gives unsatisfying results for sharp diagrams and graphics