Sub topic 4
JPEG: photographs and complex images
JPEG is the long-standing format for photographs on the web. This chapter covers how it compresses an image, which settings affect the result, and where it fails. It assumes you know what pixels and screen density are, the difference between raster and vector images, and the difference between lossy and lossless compression, all from earlier chapters. You should also be comfortable exporting from a design tool such as Figma, Sketch, or Photoshop.
What JPEG is good at
JPEG (also written JPG, with the same meaning) is a raster format that uses lossy compression. It was designed for images with smooth gradients and many subtle color changes, such as photographs, textured backgrounds, and complex illustrations with soft shading.
It works well for these images for two reasons:
- Photographs contain a lot of detail that the eye does not notice. JPEG throws that detail away first.
- Photographs have few hard edges. JPEG's errors show up mainly along sharp edges, so photos hide them well.
JPEG does not support transparency or animation. If an image needs either one, see the later chapter on transparency and animation across formats.
How JPEG compresses an image
The encoder follows several steps. You never run these by hand, but knowing them explains the artifacts you will see.
- Color conversion. The image is converted from red, green, and blue (RGB) into one brightness channel and two color channels.
- Chroma subsampling. Human eyes are more sensitive to brightness than to color detail, so the encoder stores the color channels at lower resolution. A setting called 4:2:0 keeps one color sample for every four brightness samples. This saves a lot of space.
- Block transform. The image is split into 8×8 pixel blocks. Each block is converted into a set of frequencies, from smooth changes to fine detail.
- Quantization. The encoder rounds the fine-detail values, and often discards them. This is the lossy step, and the quality setting controls how aggressive it is.
- Entropy coding. The remaining values are packed efficiently. This step loses nothing.
The quality setting
Every export tool offers a quality value, usually from 0 to 100. It controls the quantization step. Lower values discard more detail and give smaller files.
The relationship between quality and file size is not linear. Dropping from 100 to 90 often cuts the file size by more than half with almost no visible change. Dropping from 80 to 70 saves much less and starts to cost visible quality.
| Quality | Typical use | Notes |
|---|---|---|
| 90–100 | Archives, images you will edit again | Large files, little visible gain over 90 |
| 75–85 | Most web photographs | Good starting range |
| 60–70 | Backgrounds, thumbnails, images under text overlays | Artifacts may appear on close inspection |
| Below 60 | Rarely appropriate | Blocking and color banding become obvious |
These numbers are starting points. Different tools use different scales, so quality 80 in one tool does not match quality 80 in another. Always judge by comparing the result on screen, as the later chapter on compression settings describes.
Common JPEG artifacts
Knowing what bad compression looks like helps you catch it.
- Blocking. Visible 8×8 squares, most obvious in smooth areas such as sky.
- Ringing. Faint halos or noise around sharp edges, such as text on a photo.
- Color bleeding. Colors smear across edges because of chroma subsampling. Saturated red text on a different background shows this clearly.
- Banding. Smooth gradients break into visible steps.
Text, logos, and thin lines in a JPEG show ringing and bleeding quickly. This is the main reason to use PNG or SVG for those images instead.
Generation loss
Each time you open a JPEG, edit it, and save it as a JPEG again, the encoder quantizes the image a second time. Errors build up, and the image degrades with each save. This is called generation loss.
To avoid it:
- Keep your original as a lossless file, such as a layered design file, a PNG, or a TIFF.
- Export the JPEG once, as the last step.
- Never use an exported JPEG as the source for another export at a different size.
Baseline and progressive JPEG
JPEG files come in two layouts.
- Baseline files load from top to bottom. The viewer sees the image fill in line by line.
- Progressive files load a blurry full image first, then sharpen it in passes.
Progressive files are often a little smaller for photographs above roughly 10 KB, and they feel faster on slow connections because something appears quickly. Some tools call this "progressive," "interlaced," or "optimized." For most web use, choose progressive.
Exporting a JPEG from the command line
The following command resizes a PNG master to 1600 pixels wide, removes metadata, and writes a progressive JPEG at quality 80. It uses ImageMagick.
magick hero.png -resize 1600x -strip -interlace Plane -quality 80 hero-1600.jpgThe options do the following:
-resize 1600xsets the width to 1600 pixels and scales the height to keep the proportions.-stripremoves metadata such as camera details and embedded thumbnails.-interlace Planewrites a progressive file.-quality 80sets the quality value.
To compare several quality values on one image, loop over them:
for q in 60 70 80 90; do
magick hero.png -resize 1600x -strip -interlace Plane -quality "$q" "hero-1600-q$q.jpg"
done
ls -l hero-1600-q*.jpgOpen the files side by side at 100% zoom and choose the lowest quality that still looks right.
Using a JPEG in HTML
Always give the displayed size so the browser can reserve space before the image loads. This avoids the page jumping around as images arrive.
<img
src="hero-1600.jpg"
width="800"
height="450"
alt="Team working at a shared desk">Here the file is 1600 pixels wide and the image displays at 800 CSS pixels. That gives a 2x image for high-density screens. A later chapter covers serving different sizes with srcset.
Edge cases and gotchas
- Screenshots and interface mockups. These contain text and sharp edges. A JPEG will look fuzzy and may be larger than a PNG. Use PNG or WebP instead.
- Flat color areas. Large areas of one color compress well in PNG but show faint noise in JPEG.
- Transparent regions. JPEG fills transparent areas with a solid color, usually white or black. Set the matte color to the page background if you must use JPEG on a colored page.
- Color space. Export in sRGB. Images in other color spaces, such as Display P3 or CMYK, can look dull or shifted in browsers. The later chapter on color space and metadata covers this.
- Orientation data. Camera photos may rely on a metadata flag to display rotated. If you strip metadata without applying the rotation first, the image can appear sideways. Many tools handle this correctly, but check the result.
- Dimensions beyond need. A 4000-pixel-wide photo shown at 800 CSS pixels wastes bandwidth. Resize before you compress.
When to choose something else
JPEG is still widely supported and a safe default for photographs. WebP and AVIF usually produce smaller files at similar quality, and the later chapters on those formats explain when to switch. Keep a JPEG as the fallback in a picture element so older browsers still show an image.
Exercise
A designer exports a screenshot of an app screen, which contains small text and thin lines, as a JPEG at quality 60. The text looks blurry with faint halos. Name the artifact, explain why it happens, and suggest a better format and one other change.
Show answer
The halos are ringing, and the blur around colored text is color bleeding. JPEG quantizes 8×8 blocks and subsamples color, and both steps damage sharp edges. A screenshot is better exported as PNG (or lossless WebP), which keeps edges exact. If a photo must stay JPEG, raise the quality to around 85 or more, but for a screenshot the format change is the real fix.
Exercise
You have hero.jpg, already exported at quality 70 and 1200 pixels wide. A developer asks for a 2400-pixel-wide version for high-density screens. What should you do, and why?
Show answer
Go back to the original lossless source and export a 2400-pixel-wide JPEG from it. Upscaling the existing 1200-pixel JPEG adds no detail, and re-saving it applies lossy compression a second time, so the result would be blurry and show generation loss.