Sub topic 13
Setting compression quality
This chapter assumes you can export from a design tool, know the difference between lossy and lossless compression, and have already picked a format for the image (JPEG, PNG, WebP, or AVIF). Here you decide how hard to compress it. The goal is the smallest file that still looks right at the size it is displayed.
What the quality number means
Lossy encoders take a quality setting, usually from 0 to 100. A higher number keeps more detail and makes a larger file. The number is not a percentage of the original, and it is not comparable between formats or between encoders. JPEG quality 80 from one tool can look different from quality 80 in another. WebP at 80 and AVIF at 80 are different amounts of compression.
Two points follow from this:
- Compare files by looking at them and by checking their size in kilobytes. Do not rely on the number alone.
- The size-to-quality curve is not a straight line. Going from 100 to 90 often cuts the file by half. Going from 80 to 70 cuts it by much less. Most of the savings come from leaving the top of the scale.
Lossless formats such as PNG have no quality number in the same sense. Their settings control how much effort the encoder spends, not how much detail is lost. Lossless PNG gets smaller mainly by using fewer colors or by removing metadata.
Starting points by format
These are starting values, not rules. Test each image.
| Format | Typical starting quality | Notes |
|---|---|---|
| JPEG | 75 to 85 | Photos usually look fine at 80. Detailed textures may need 85. |
| WebP (lossy) | 75 to 80 | Often 25 to 35 percent smaller than JPEG at similar looks. |
| AVIF | 50 to 65 | Its scale is different. Values that look low still often look good. |
| PNG | Not applicable | Reduce colors (palette PNG) or dimensions to save bytes. |
| WebP (lossless) | Not applicable | A good replacement for PNG when the browser supports it. |
Find the setting by comparing
The method is the same for every image:
- Export at a high quality, such as 90, as your reference.
- Export again at lower steps, such as 80, 70, and 60.
- View each at 100 percent zoom, at the size it will really be displayed.
- Pick the lowest quality where you cannot see a difference from the reference without hunting for it.
- Write down the number and the file size.
Many design tools show a live preview with the file size next to it. Use the split or 2-up view if there is one.
You can also script the steps. This command makes four JPEG files from one source so you can compare them side by side.
for q in 90 80 70 60; do
magick hero.png -resize 1600x -strip -quality $q "hero-1600-q$q.jpg"
done
ls -l hero-1600-q*.jpgFor WebP, the same loop works with cwebp:
for q in 90 80 70 60; do
cwebp -q $q hero-1600.png -o "hero-1600-q$q.webp"
done
ls -l hero-1600-q*.webpWhat damage looks like
Know what to look for, so you can judge a preview quickly.
- Blocking. Visible 8 by 8 pixel squares, mostly in smooth areas such as sky or skin. This is typical of low-quality JPEG.
- Ringing. Halos or fuzzy specks around sharp edges, especially around text or lines on a photo.
- Banding. Smooth gradients turn into visible stripes. This often appears first in dark gradients and in AVIF or WebP at low quality.
- Color bleeding. Saturated colors, such as a red logo on a blue field, smear into their neighbors. Lossy formats store color at lower resolution than brightness, which causes this.
- Smoothing. Skin and fabric lose texture and look waxy. WebP and AVIF are more prone to this than JPEG at the same file size.
Check the areas where damage shows first: gradients, text over photos, faces, and fine texture. A photo of a crowd hides artifacts. A photo of a clear sky does not.
Different images need different settings
One quality value for a whole site is a common mistake. Content decides how much compression it tolerates.
- Busy, detailed photos hide artifacts well. They can often go lower.
- Smooth gradients, studio backdrops, and product shots on plain backgrounds show artifacts early. They need higher quality.
- Images with text or sharp lines are poor candidates for lossy formats at all. Use PNG, lossless WebP, or SVG.
- Large hero images deserve more attention than small thumbnails, because they cost more bytes and are more visible.
- Images displayed small, such as avatars and thumbnails, often tolerate lower quality because the artifacts are smaller than the eye can resolve.
2x images can use lower quality
A 2x image has twice as many pixels in each direction, so each pixel is shown smaller on a dense screen. Compression artifacts are therefore harder to see. A 2x JPEG at quality 60 to 65 often looks as good as a 1x JPEG at quality 80, and the 2x file can end up the same size or smaller than a 1x file exported at a high quality. If you serve 2x files, test lower settings for them.
Further reductions beyond quality
Quality is only one lever. Before lowering it further, check these:
- Dimensions. Exporting at 3200 pixels wide for a 800-pixel slot wastes more than any quality setting saves. Fix size first.
- Metadata. Strip camera data, thumbnails, and color profiles you do not need. The
-stripflag in the commands above does this. Be careful with ICC profiles: convert to sRGB first, then strip, or colors can shift. - Chroma subsampling. Most JPEG encoders store color at half resolution by default. This is fine for photos. For images with sharp colored edges, a setting such as 4:4:4 keeps them clean at the cost of a larger file.
- Progressive encoding. A progressive JPEG loads as a blurry full image that sharpens, instead of loading top to bottom. It is often slightly smaller as well.
- Palette PNG. A PNG with 256 or fewer colors can be much smaller than a full-color PNG, with no visible change on flat graphics.
Record your settings
Once you have settings that work, write them down for the team: format, quality, and expected size range per image type. A short table in the handoff notes saves repeated trial and error. It also helps developers who regenerate images in a build step to match what you approved.
Exercise
You export a product photo on a plain light-grey backdrop as a JPEG at quality 60 and the file is small, but the backdrop shows faint stripes. What are two changes you could try, and why?
Show answer
Raise the quality, for example to 80 or 85, because smooth gradients in plain backdrops show banding and blocking at low quality. Alternatively, keep the quality and switch to a format or setting that handles gradients better, such as WebP at 80 or JPEG with 4:4:4 chroma subsampling. In each case, compare at 100 percent zoom and check the file size against your budget.
Exercise
A teammate says "use quality 80 for every image on the site." Give two reasons why a single value is a poor rule.
Show answer
Quality numbers are not comparable between formats, so 80 means different things in JPEG, WebP, and AVIF. Also, images differ in how well they hide artifacts: a busy photo may look fine at 65, while a smooth gradient may need 85. Each image type should be tested and given its own setting.