Skip to content
PDFLove

how does jpeg compression work

How JPEG Compression Works

JPEG works by discarding information your eye is bad at noticing. Understanding which information explains why quality 80 looks fine, why text gets halos, and why re-saving is corrosive.

01

Step one: split brightness from colour, then throw colour away

The image is converted from red-green-blue into a brightness channel and two colour-difference channels. This is not compression yet — it is a reorganisation that separates what your eye is good at from what it is not.

Human vision resolves fine detail in brightness far better than in colour. JPEG exploits this immediately with chroma subsampling: the colour channels are stored at half resolution in each direction, which discards three quarters of the colour data before any of the clever mathematics has run. On a photograph, this is essentially invisible.

On a screenshot with red text on white, it is very visible indeed, and it is the reason text in a JPEG picks up coloured fringes. There was never enough colour resolution left to place a hard red edge.

02

Step two: turn 8×8 blocks into frequencies

Each channel is cut into 8×8 pixel blocks, and each block is transformed with a discrete cosine transform. The output is not pixels. It is 64 coefficients describing how much of each spatial frequency the block contains — one for the average brightness, then progressively finer patterns of variation.

For most real-world blocks the energy piles up in the first few coefficients. A patch of sky is almost entirely its average value with tiny amounts of everything else. This step is lossless and reversible; it has not saved a byte. What it has done is sort the information by how much it matters.

03

Step three: quantization — this is the whole ballgame

Each of the 64 coefficients is divided by a value from a quantization table and rounded to an integer. Small coefficients become zero. This is the only lossy step in JPEG, and it is exactly what the quality slider controls: quality 90 uses a gentle table, quality 30 a brutal one.

The tables are shaped to divide the high-frequency coefficients hardest, because that is the fine detail your eye is least likely to miss. After quantization a typical block that started with 64 non-zero numbers has five or six, the rest being zeros — and a long run of zeros is exactly what the final stage compresses to almost nothing.

Everything people notice about JPEG comes from this step. Blockiness at low quality is 8×8 blocks whose coefficients were flattened so far that neighbouring blocks no longer agree at their shared edge. Ringing around text is the high-frequency coefficients needed to describe a hard edge being quantized away, leaving a ripple. Both are the algorithm working as designed on content it was not designed for.

Quality 90–100
Nearly lossless, and large. Worth it only for images you will edit again.
Quality 75–85
The sweet spot. Typically a third of the size of 100 and hard to tell apart.
Quality 50–70
Visible on close inspection. Fine for thumbnails, not for anything with text.
Below 50
Blocking and ringing are obvious. The saving over 75 is much smaller than the damage.

04

Step four: pack it, losslessly

The quantized coefficients are read out in a zigzag order that runs from low frequency to high, which groups all those zeros into one long run at the end. That run is collapsed by run-length coding, and the result is entropy-coded with Huffman coding.

This stage is completely lossless and typically responsible for the last large factor of reduction. Nothing here degrades the image; it is just efficient packing of the numbers the previous step left behind.

05

Generation loss: why re-saving is corrosive

Open a JPEG, change nothing, save it again, and the file is not identical. Decoding reconstructs pixels from quantized coefficients; re-encoding runs those reconstructed pixels through the DCT and quantizes again. The second quantization rounds numbers that were already rounded, and the error compounds.

One extra generation at high quality is barely detectable. Ten are obvious — this is the mechanism behind every image that has been screenshotted, saved and reshared until it looks like it was transmitted by fax.

The practical rule follows directly. Keep the original. Edit from the original rather than from an exported copy. And when a file needs to be smaller, resize before you re-encode: dropping the pixel dimensions removes data cleanly, while lowering quality on full-size pixels removes it destructively.

06

When JPEG is the wrong choice

Everything above assumes photographic content: smooth gradients, no hard edges, no exact repetition. Give JPEG the opposite and every design decision works against you. A screenshot, a logo, a chart, a line drawing — these are large flat areas and sharp boundaries, which is precisely the content chroma subsampling smears and quantization rings.

PNG is built for that case. It compresses losslessly by predicting each pixel from its neighbours and encoding the difference, which is spectacularly effective on flat colour and useless on photographs. That is the whole rule: continuous tone goes to JPEG, flat colour and hard edges go to PNG, and anything needing transparency has to be PNG or WebP because JPEG has no alpha channel at all.

FAQ

Related questions

Why does text look bad in a JPEG?

Two mechanisms compound. Chroma subsampling halves colour resolution before anything else runs, and quantization discards the high-frequency coefficients that a sharp edge is made of. Text is nothing but sharp edges, so it gets both — coloured fringes and a ripple around every letter.

What does the JPEG quality number actually mean?

It selects a quantization table. Higher quality divides the coefficients by smaller numbers, so fewer are rounded to zero and more detail survives. It is not a percentage of anything, which is why the difference between 100 and 90 is negligible and the difference between 60 and 50 is not.

Can I recover quality from an over-compressed JPEG?

No. Quantization rounds coefficients to zero and there is no record of what they were. Saving at quality 100 afterwards produces a larger file containing exactly the same damage.

Is WebP just a better JPEG?

For lossy photographs it is roughly 25–35% smaller at matching quality, and it also does lossless and transparency, which JPEG cannot. The catch is acceptance — plenty of upload forms and older software still take only JPEG and PNG.