Sub topic 7
Lossy and lossless compression
This chapter assumes you know that a raster image is a grid of pixels, and that each pixel stores color values such as red, green and blue. It also assumes you can read short Python snippets. Compression is the step that turns the large pixel grid into a file of practical size.
Why compression exists
An uncompressed raster image stores every pixel in full. The size grows with the pixel count, as the calculation below shows.
width, height = 1920, 1080
bytes_per_pixel = 3 # 8 bits each for red, green and blue
size_bytes = width * height * bytes_per_pixel
print(size_bytes) # 6220800 bytes, about 5.9 MiB uncompressedA single Full HD photo would take almost 6 MiB. A web page with ten of them would be slow to load. Compression shrinks the data by finding a shorter way to describe it. There are two families, and they differ in one question: can you get the original pixels back exactly?
Lossless compression
Lossless compression removes redundancy without discarding any information. Decoding gives back every pixel exactly as it was.
The simplest example is run-length encoding. A row of 200 white pixels can be stored as "white, 200 times" instead of 200 separate values. Real formats use stronger methods. PNG, for example, predicts each pixel from its neighbors and then packs the leftover differences with the DEFLATE algorithm.
Lossless compression works best on images with large flat areas, sharp edges and repeated patterns, such as screenshots, logos, diagrams and pixel art. It works poorly on photographs, because sensor noise and fine texture leave little repetition to exploit.
Lossy compression
Lossy compression permanently discards some information. It chooses details that human vision notices least, so the result looks almost the same while the file is much smaller.
JPEG shows the typical steps:
- Convert the colors into brightness plus color-difference channels.
- Reduce the resolution of the color channels, because the eye is less sensitive to color detail than to brightness detail.
- Split the image into 8×8 pixel blocks and transform each block into frequency components.
- Round away the high-frequency components, which carry fine detail. This is the lossy step, and the quality setting controls how aggressive it is.
- Pack the remaining numbers with a lossless coder.
Comparing the two
| Property | Lossless | Lossy |
|---|---|---|
| Original pixels recoverable | Yes | No |
| Typical size reduction on photos | Modest | Large |
| Best content | Flat color, text, sharp edges | Photos, smooth gradients |
| Repeated saving | No quality change | Quality drops each time |
| Typical formats | PNG, GIF, lossless WebP | JPEG, lossy WebP |
Common errors
Resaving a JPEG repeatedly. Each save re-quantizes the image and discards more detail. Errors accumulate, a problem called generation loss. Keep an original or lossless master and export JPEG copies from it once.
Using JPEG for sharp graphics. At the block edges you will see ringing, which looks like faint halos and smudges around text and hard lines. A screenshot saved as JPEG is often both larger and uglier than the same screenshot saved as PNG.
Using PNG for photographs. The file will be correct but large, because lossless compression cannot shrink noisy detail much.
Assuming quality 100 means lossless. A JPEG at quality 100 is still lossy. It simply discards very little.
Editing from a lossy file. Treat a lossy file as a delivery format. Do your editing on a lossless source.
Vector files are a separate case. SVG stores shape descriptions as text, so size depends on the number of shapes, not on pixel count. It can still be compressed with a lossless method such as gzip when served on the web.
Exercise
A designer exports a company logo with flat colors and sharp text as a JPEG at quality 60, then opens and resaves it three times to add small edits. Name two problems this causes and say what to do instead.
Show answer
The JPEG adds visible artifacts such as halos around the text and edges, and each resave discards more detail through generation loss. Instead, keep the logo as a vector or lossless master (SVG, or PNG for raster), make edits there, and export once to the delivery format.
Exercise
A 1920×1080 image uses 3 bytes per pixel. Its uncompressed size is 6,220,800 bytes. If lossy compression brings it to 311,040 bytes, how many times smaller is the file?
Show answer
6,220,800 / 311,040 = 20, so the file is 20 times smaller.