Algorithms, visualizedExhibit 22
How does a JPEG make photos so small?
A JPEG keeps what your eyes notice and rounds away the rest. It stores color at half resolution, rewrites each 8 by 8 block as 64 patterns, and turns the fine ones into zeros. Our 720,000-byte photo became 41,606 bytes: 94% smaller.
Try it: drag the quality down
This is the photo from our video, 600 by 400 pixels. Your browser saves it as a JPEG at the quality you pick and shows the result, enlarged on the right. Or use a photo of your own: it stays on your device.
Your browser's JPEG encoder is not the one we used for the video, so its file sizes differ a little from ours. The steps are the same.
The video for this exhibit comes out on YouTube on Friday 5 February. Subscribe so you don’t miss it
Watch: how a JPEG makes photos small
One real photo, 94% smaller, and what happened after 100 re-saves.
The video loads from YouTube only when you press play. Watch on YouTube
Color first
You see fine detail in brightness much better than in color. So a JPEG splits the picture into one brightness layer and two color layers, and usually keeps the color layers at half the width and half the height. Three numbers per pixel become one and a half: half the data is gone before the main step.
Eight by eight
The picture is cut into blocks of 8 by 8 pixels. Each block is rewritten as a mix of 64 patterns: one for the average brightness, then gentle waves, then finer and finer stripes and checks. Most of a real photo is smooth, so the fine patterns barely matter. The method is the discrete cosine transform, published by Ahmed, Natarajan and Rao in 1974.
The rounding
This is where data is really lost. Each of the 64 numbers is divided and rounded, gently for the coarse patterns and hard for the fine ones, so small numbers become zero. In the brightness layer of our photo, at quality 50, an average block kept 9 numbers out of 64, and 86% of all the numbers became zeros. Long runs of zeros are then packed very compactly.
What we measured
Our photo is 720,000 bytes as raw pixels. As a lossless PNG it is 441,801 bytes (39% removed). As a JPEG it is 104,711 bytes at quality 95 (6.9 times smaller than raw), 41,606 at quality 75 (17.3 times, or 94% smaller) and 9,680 at quality 10 (74.4 times), where the blocks start to show. Re-saving the same file 100 times at the same setting barely changed it after the first save (average error 4.02, then 4.10 out of 255). Shifting the picture one pixel before every save, a deliberate stress test, raised the error to 31.37 and wrecked it.
When not to use it
Sharp edges are exactly the fine patterns a JPEG throws away. We saved a 600 by 400 picture of plain black text both ways: the PNG was 17,823 bytes and exact; the JPEG was 18,077 bytes and changed 6.7% of the pixels. Photos suit JPEG; text, line art and screenshots suit PNG.
Frequently asked questions
How does JPEG compression work?
It stores color at lower resolution than brightness, cuts the picture into 8 by 8 pixel blocks, rewrites each block as a mix of 64 patterns, and rounds away the fine patterns that the eye barely notices.
Why do JPEGs get blocky?
Each 8 by 8 block is compressed on its own. At low quality almost all of a block's fine detail is rounded to zero, so neighbouring blocks stop matching at their edges and the grid becomes visible.
Does re-saving a JPEG make it worse every time?
In our test, re-saving the same file 100 times at the same quality barely changed it after the first save. Shifting the picture by one pixel before every save, so the blocks never line up, wrecked it.
Should I use JPEG or PNG?
JPEG for photographs. PNG for text, line art and screenshots: in our test a picture of black text was slightly smaller as a PNG and exact, while the JPEG changed 6.7% of the pixels.
Sources
- Wikipedia: JPEG
- Wikipedia: Chroma subsampling
- Wikipedia: Discrete cosine transform
- scikit-image sample data: the coffee photo (CC0, Rachel Michetti)
Last updated
Keep exploring

How does autocorrect know what you meant?
Type a misspelling and watch the candidate words line up by how common they are.
Why do damaged QR codes still scan?
Paint over a real QR code until it stops reading. Then cover just one corner.
How does Google search so fast?
Search real books two ways and watch the timer.See all the exhibitsSubscribe on YouTube (opens in a new tab)