SpunkArt
Color, art and images

How image compression works and how to shrink images

Images are often the heaviest files on a web page, and many are far bigger than they need to be. Shrinking them is mostly about two choices: the pixel dimensions, and the format and quality you save with.

Start with the raw size

An uncompressed color image stores three bytes per pixel, one each for red, green and blue (four with transparency). That gives a simple formula:

raw bytes = width × height × bytes per pixel

Example: a 12-megapixel photo. A 4000 × 3000 image has 12,000,000 pixels. At 3 bytes each that is 36,000,000 bytes, about 34.3 MiB (dividing by 1,048,576).

Resize it to 1600 × 1200 for a web page and the raw size drops to 5,760,000 bytes. The pixel count falls by a factor of 12,000,000 ÷ 1,920,000 = 6.25, before any compression at all.

That is the first lesson: resize before you compress. No quality setting saves as much as not sending pixels nobody will see.

Lossless and lossy

Lossless compression finds patterns and stores them more briefly, like writing “200 × white” instead of white two hundred times. Decompress it and you get every original pixel back. PNG and GIF are lossless, and WebP and AVIF have lossless modes.

Lossy compression throws away detail the eye is unlikely to notice, then compresses what is left. JPEG is the classic lossy format. It gives far smaller photos, but each re-save loses a little more.

What JPEG does to a photo

  1. Splits brightness from color. The image is converted from RGB to one brightness channel and two color-difference channels.
  2. Reduces color detail. Eyes are less sharp for color than for brightness. With the common 4:2:0 setting, each color channel keeps one value per 2×2 block of pixels. Per pixel that is 1 + ¼ + ¼ = 1.5 values instead of 3, half the data before the next step.
  3. Works in 8×8 blocks. Each block is turned into a set of frequency values: smooth changes and fine detail.
  4. Rounds the fine detail. The quality setting controls how coarsely those values are rounded. Low quality rounds hard, which causes blockiness and halos around edges.
  5. Packs the result losslessly.

Choosing a format

FormatTypeBest forWatch out for
JPEGLossyPhotos, paintings, gradientsNo transparency; halos on sharp text
PNGLosslessScreenshots, flat graphics, pixel art, transparencyLarge files for photos
WebPLossy or losslessSmaller replacement for both of the aboveSome older software cannot open it
AVIFLossy or losslessSmallest photos at good qualitySlower to encode
SVGVectorLogos, icons, simple diagramsNot suitable for photos

A practical workflow

  1. Find the display size. If an image shows at most 800 CSS pixels wide, export about 1600 pixels wide to look sharp on high-density screens, and no larger.
  2. Pick the format from the table above.
  3. Export at a middle quality, then compare it with the original at 100% zoom. Lower the quality until you can see a difference, then go back one step.
  4. Look at the hard areas: skies, skin, text and sharp edges show artefacts first.
  5. Keep the original. Always compress from the master file, never from an already-compressed copy.

Two command-line examples, using free tools:

magick painting.jpg -resize 1600x1600 -quality 82 painting-web.jpg
cwebp -q 80 painting.png -o painting.webp

The ImageMagick -resize 1600x1600 option fits the image inside that box and keeps its shape. The quality numbers are examples; judge the result by eye.

Common mistakes

  • Saving screenshots as JPEG. Text and flat color get fuzzy edges. PNG or lossless WebP is usually both sharper and smaller.
  • Re-saving JPEGs repeatedly. Each round adds more loss.
  • Relying on CSS to shrink a huge file. A 4000-pixel image squeezed into 400 pixels still downloads in full.
  • Stripping the wrong metadata. Removing camera data saves bytes, but keep the color profile if colors shift without it, and keep copyright fields if you need them.

Questions people ask

Is quality 100 lossless?

No. A JPEG at its highest quality setting still goes through the lossy steps, just with very fine rounding. Use a lossless format if you need every pixel exact.

Should I use one format for everything?

You can use the HTML picture element to offer AVIF or WebP with a JPEG fallback. The browser picks the first format it supports.