Article
Compressing images without losing anything
A plain white web page, a company logo and a chart are mostly big areas of one colour. A picture like that holds the same pixel again and again. If a computer wrote "white" a million times, it would be wasting bits.
The run-length idea from the last article works on pictures too, and it is still lossless: every pixel comes back exactly as it was.
A picture is a long line of pixels
You already know that a picture is a grid of pixels, and that each pixel is stored as bits. To compress it, we read the grid row by row, from the top row to the bottom row and from left to right in each row. This turns the grid into one long line of pixels. Then we find the runs in that line. The next sections work through these four steps:
Compressing a picture without losing anything
Grid of pixels
48 pixels, 12 in each row
read row by rowOne long line
48 pixels in order
find the runsRuns
14W 8B 4W 8B 14Wcount and colour in bitsBits
25 bits instead of 48
To keep the example small, here is a black-and-white picture. W is a white pixel and B is a black pixel. Each pixel needs 1 bit, so a picture with 48 pixels is 48 bits.
W W W W W W W W W W W W
W W B B B B B B B B W W
W W B B B B B B B B W W
W W W W W W W W W W W WRead row by row, the long line is this:
| Row | Pixels in that row |
|---|---|
| 1 | 12 white |
| 2 | 2 white, 8 black, 2 white |
| 3 | 2 white, 8 black, 2 white |
| 4 | 12 white |
Notice that a run can go across the end of a row. The 12 white pixels of row 1 are followed by the 2 white pixels at the start of row 2, so together they are one run of 14 white pixels.
Counting the runs
Join the rows and look for runs of the same colour:
- Row 1 (12 white) and the start of row 2 (2 white): 14 white.
- The black middle of row 2: 8 black.
- The end of row 2 (2 white) and the start of row 3 (2 white): 4 white.
- The black middle of row 3: 8 black.
- The end of row 3 (2 white) and all of row 4 (12 white): 14 white.
The picture is stored as 14W 8B 4W 8B 14W. Check by adding the counts: 14 + 8 + 4 + 8 + 14 = 48 pixels. Nothing is missing.
Check your understanding
Here is a black-and-white picture (B is black, W is white).
B B B B
B W W B
B B B BRead the pixels row by row as one long line, and let a run continue across the end of a row. Which run list stores the picture? Each run is a count and then a colour.
Worked example: how many bits?
Each run needs a count and a colour.
- The longest run is 14, and 14 in binary is
1110. So 4 bits are enough for any count up to 15. - The colour needs 1 bit (for example 0 for white and 1 for black).
- One run costs 4 + 1 = 5 bits.
Now compare the sizes:
| Size | |
|---|---|
| Original: 48 pixels at 1 bit each | 48 bits |
| Compressed: 5 runs at 5 bits each | 25 bits |
The ratio is 48 ÷ 25 = 1.92, about 1.9 to 1. The percentage saved is 23 ÷ 48, about 48%. To decompress, read each run and write that many pixels of that colour. Then cut the long line back into rows of 12. The picture is exactly the same.
Colour pictures and real life
A colour pixel needs 24 bits when it uses three 8-bit numbers (red, green and blue). The idea is the same: a run of 500 identical colour pixels can be stored as one count and one colour, instead of 500 colours.
Whether this works depends on the picture:
- Logos, charts, screenshots and cartoons have big areas of one colour. They have long runs, so they shrink a lot.
- Photos change a little at almost every pixel because of light and tiny noise. There are few long runs, so a lossless method saves much less.
Picture formats such as PNG are lossless. They use cleverer methods than plain RLE, but they still depend on repeats.
Check your understanding
Which picture would a lossless run-length method shrink the most?
Common mistakes
- Stopping a run at the end of a row. In the long line, the last pixels of one row can join the first pixels of the next.
- Grouping all the pixels of one colour together. A run must follow the pixels in order. The same colour can appear in several runs.
- Forgetting the colour.
14alone does not say white or black. - Expecting a photo to shrink as much as a logo. Few long runs means little saving.
On the exam
You may get a small grid or a list of runs and be asked to work out the compressed size, or what the picture was. You may also be asked which kind of picture compresses well. Say that lossless compression finds repeats, so it works best where data repeats a lot.
Key words
Pixel: one point of a picture.
Run: a group of identical pixels one after another.
Lossless: a method where the exact original can be rebuilt, so every pixel is the same.