Sub topic 3
Lossy versus lossless compression
This chapter assumes you can export an image from a design tool and know that a raster image is a grid of pixels. It explains the one choice that sets most file sizes: whether the encoder may change your pixels.
What compression does
An exported image file is a compressed description of a pixel grid. The compressor (the encoder) looks for ways to describe the grid in fewer bytes. The browser's decoder then rebuilds the pixels for display.
There are two families of compression.
- Lossless compression rebuilds exactly the pixels you put in. Every color value matches the original.
- Lossy compression rebuilds an approximation. The encoder throws away information it predicts you will not notice, and that information cannot be recovered.
How lossless compression saves space
Lossless encoders find patterns and describe them briefly. A row of 300 identical white pixels can be stored as "white, 300 times". Repeated shapes can be stored once and referenced. Nothing is discarded, so the savings depend on how repetitive the image is.
This is why a flat-color interface screenshot shrinks well and a photograph does not. A photo has sensor noise and gentle gradients, so almost no two neighboring pixels are identical. A lossless file of a photo is often several times larger than a lossy one.
How lossy compression saves space
Lossy encoders use facts about human vision.
- We notice changes in brightness more than changes in color, so encoders store color at lower detail than brightness (chroma subsampling).
- We notice fine, high-frequency detail less than broad shapes, so encoders simplify tiny variations first.
- Encoders work on blocks of pixels. They round the numbers that describe each block, and coarser rounding makes a smaller file.
The quality setting controls how coarse the rounding is. A lower setting gives a smaller file and more visible damage.
What lossy damage looks like
Learn to recognize these artifacts (visible defects caused by compression). You will use them when you set quality in a later chapter.
| Artifact | What you see | Where it shows up first |
|---|---|---|
| Blocking | Visible square tiles, often 8×8 or 16×16 pixels | Smooth skies, skin, shadows |
| Ringing or mosquito noise | Fuzzy specks around hard edges | Text and logos on a photo |
| Banding | Stripes where a smooth gradient should be | Dark gradients, backgrounds |
| Color bleeding | Red or blue fringes leaking past an edge | Thin red lines, colored text |
Artifacts are worst on sharp edges and flat color. This is why a lossy format suits a photograph and is a poor fit for a screenshot of text.
Which formats are which
Compression type belongs to the encoding choice, and some formats offer both.
| Format | Lossless | Lossy | Notes |
|---|---|---|---|
| JPEG | No (in practice) | Yes | Always lossy on the web |
| PNG | Yes | No | Always lossless |
| GIF | Yes | No | Limited to 256 colors per frame |
| WebP | Yes | Yes | You choose a mode at export |
| AVIF | Yes | Yes | Usually used in lossy mode |
| SVG | Not applicable | Not applicable | Text description of shapes, not pixels |
Two details cause confusion. First, GIF is lossless in how it stores pixels, but reducing a photo to 256 colors discards a lot beforehand. Second, vector files such as SVG do not store pixels, so the question does not apply to them. They stay sharp at any size.
Measuring the trade-off in practice
You can see the trade by encoding one source three ways. These commands use ImageMagick and the WebP command-line tools. Start from a lossless master named hero.png.
magick hero.png -quality 80 hero-q80.jpg
cwebp -q 80 hero.png -o hero-lossy.webp
cwebp -lossless hero.png -o hero-lossless.webp
wc -c hero.png hero-q80.jpg hero-lossy.webp hero-lossless.webpThe wc -c command prints the byte size of each file. On a typical photograph, the lossy files come out at a small fraction of the PNG, and the lossless WebP lands a little below the PNG. On a flat-color graphic, the gap between lossy and lossless is much smaller.
You can also prove that a file is lossless. Decode it and count the pixels that differ from the master.
dwebp hero-lossless.webp -o roundtrip.png
magick compare -metric AE hero.png roundtrip.png null: ; echoThe AE metric counts differing pixels, so a lossless round trip prints 0. Run the same comparison against hero-q80.jpg and you will get a large number. Add -metric PSNR for a quality score in decibels, where higher means closer to the original.
Generation loss
Lossy damage accumulates. If you open a JPEG, edit it, and save it as JPEG again, the encoder discards detail a second time on top of the first loss. After several rounds, blocking and color shifts become obvious. This is called generation loss.
The same applies when you convert between lossy formats. A JPEG converted to WebP or AVIF carries its old artifacts forward, and the new encoder adds its own. You also gain less than you expect, because the encoder spends bytes describing the old damage.
Follow these habits to avoid it:
- Keep a lossless master, such as a layered design file, a PNG, or a TIFF.
- Export each web version from the master, never from a previous export.
- Resize first, then compress once. Resizing a lossy file and saving it again adds a second round of loss.
- Do not "optimize" an already-compressed JPEG again with a lower quality setting unless you have no master.
Choosing between them
Use these questions as a first filter. Later chapters turn them into a full decision process.
- Does the image contain photographic detail, soft gradients, or noise? Lossy usually wins by a wide margin.
- Does it contain text, thin lines, sharp UI edges, or flat color? Lossless avoids artifacts, and the files are often already small.
- Must the pixels be exact, such as a screenshot used in documentation or an image you will edit again? Use lossless.
- Is the file a logo or icon made of shapes? Prefer a vector format and skip the question.
Lossless is not automatically "better quality" in a way users notice. A well-tuned lossy photograph looks the same to most viewers at a fraction of the weight, and that weight affects load time on slow connections.
Exercise
You export a product photo as a PNG and the file is 2.4 MB. A teammate re-saves the PNG as a JPEG at quality 85, edits the caption area, and saves it as JPEG again at quality 85. Name the compression type of each format, say what has happened to the pixels, and say what you would do instead.
Show answer
PNG is lossless and JPEG is lossy. The first JPEG save discarded detail permanently. The second save discarded more on top of that, which is generation loss, so artifacts such as blocking or ringing around the caption may now show. Instead, keep the PNG or layered file as the master, make the caption edit there, and export one JPEG from it at a chosen quality, after resizing to the displayed dimensions. A photograph also suits a lossy format, so the JPEG (or lossy WebP) is the right delivery file, just not one that gets re-saved.