Sub topic 3
Lossy versus lossless compression
This chapter assumes you can export from a design tool and know that JPEG and PNG are image formats. It explains the two ways a format can make a file smaller. Later chapters rely on this split when they compare formats.
What compression does
Compression stores the same picture in fewer bytes. There are two approaches.
- Lossless compression finds patterns and writes them more compactly. Decoding returns every pixel exactly as it was exported.
- Lossy compression permanently discards detail that people are unlikely to notice. Decoding returns an approximation of the original.
Lossless is like zipping a document. Lossy is like summarizing it: shorter, but you can't rebuild the original from the summary.
How lossy compression hides its work
Lossy encoders exploit how vision works. We notice small changes in brightness more than small changes in color, and we notice fine detail less in busy areas. The encoder removes information in those places first.
Pushing quality too low causes visible artifacts:
- Blocky squares in smooth gradients, called banding or blocking.
- Fuzzy "mosquito noise" around sharp edges and text.
- Muddy color in areas that were subtle.
Photographs hide these flaws well because they contain texture and noise. Screenshots, logos, and text do not. A lossy encoder smears the hard edges, and the damage is easy to see.
Generation loss
Each time you re-save a lossy file, the encoder discards more detail. Errors accumulate. For that reason, keep an original or master file, and export from the master every time. Never re-export from a previous JPEG.
Comparing the two
| Property | Lossless | Lossy |
|---|---|---|
| Pixel accuracy | Exact | Approximate |
| Typical file size | Larger | Much smaller for photos |
| Best for | Flat color, text, sharp edges | Photographs, soft gradients |
| Quality control | Usually none, or an effort setting | A quality slider |
| Repeated saving | Safe | Degrades the image |
Some formats offer both modes. WebP, for example, can encode either way, as the next commands show.
cwebp -lossless hero-1600.png -o hero-lossless.webp
cwebp -q 80 hero-1600.png -o hero-lossy.webpRun both and compare the file sizes. For a photograph, the lossy file is usually far smaller. For a flat-color illustration, the gap is often small, and the lossless file may look better.
Common mistakes
- Saving a logo or UI screenshot as a low-quality lossy file, which produces fuzzy edges.
- Saving a large photograph losslessly, which produces a file many times larger than needed.
- Judging quality while zoomed out. Check at 100 percent, because artifacts show most at full size.
Exercise
A designer exports a product photo as a lossy file, then opens that file, crops it, and exports it as lossy again. Why is this a problem, and what should they do instead?
Show answer
Each lossy save discards more detail, so the second export adds new artifacts on top of the first. They should keep the original master and export the cropped version from it in a single step.