Shrinking Pictures: Compression
About this lesson, for parents and teachers
A picture on a computer is a grid of pixels, and storing every single one takes a lot of space. What if you could just say "8 white" instead of "white, white, white..."? Paint and find out.
Ages 9–11 · about 7 minutes · builds on Pictures Are Made of Pixels
Try this
- Read the numbers next to each row and paint the picture they describe. White always comes first.
- Paint your own picture with at least 12 coloured pixels that fits in 16 numbers or fewer.
- Watch the meter: the green bar is the code, the grey bar is the 64 raw pixels.
- Try to make the code longer than 64 numbers. What kind of picture does that?
How it works
This picture has 8 rows of 8 pixels, so storing every pixel takes 64 numbers. But most pictures have long runs of the same colour. Instead of listing each pixel, we can count them: a row that is all white is just 8. A photo from a phone has millions of pixels, so saving space really matters.
This trick is called run-length encoding, or RLE. Each row is written as counts that take turns: white, then colour, then white, and so on. The row 2, 4, 2 means 2 white, 4 coloured, 2 white. If a row starts with colour, the first count is 0 white. Because the counts always take turns, the computer never needs to write down the colours, only the numbers.
Turning the picture into numbers is encoding (compressing). Turning the numbers back into the picture is decoding (decompressing). Nothing is lost: the heart comes back pixel-perfect. That makes RLE a lossless compression. Some formats, like JPEG photos, are lossy: they throw away tiny details you would not notice to save even more space. RLE never throws anything away, so the picture you decode is exactly the picture you encoded.
Compression is not magic. A picture with stripes or a checkerboard changes colour at almost every pixel, so the code gets longer than the picture itself. Every compression method has pictures it cannot shrink. Real formats like PNG use cleverer tricks than RLE, but they rely on the same idea: find patterns that repeat and describe them in fewer numbers.
Where engineers use this
Fax machines used run-length encoding to send black-and-white pages over phone lines, and PNG and ZIP files use pattern-finding compression so photos and downloads take less space and arrive faster.
Words to know
- Pixel
- One tiny square of colour in a digital picture.
- Compression
- Storing the same information using fewer numbers or bits.
- Run-length encoding
- Compressing by counting how many of the same thing come in a row, like 5 white then 3 black.
- Lossless
- Compression where you get back exactly the original, with nothing missing.
Talk about it
Goal: Squeeze a picture into fewer numbers by counting runs of the same color.
- Why do you think a photo of a blue sky makes a smaller file than a photo of a busy crowd?
- How would you describe a striped flag to a friend in as few words as possible?
- Is there any picture that cannot be squeezed at all?
Without a screen: Use squared paper. One person secretly colours a small picture and writes the run-length numbers for each row (white first). The other person decodes the numbers and colours their own grid. Compare: are they identical? This is the CS Unplugged image representation activity.
Curriculum links: CSTA K-12 1B-DA-06 (organize and present collected data to highlight relationships) and 1A-DA-05 (store, copy, search, retrieve, modify and delete information); AP Computer Science Principles DAT-1.D (lossless data compression)
Questions
What is run length encoding?
Run-length encoding stores data by counting repeats. Instead of writing white, white, white, black, it writes 3 white, 1 black. It is simple and works very well when the same value repeats a lot.
How do files get smaller when you zip them?
Compression programs look for patterns that repeat and replace them with shorter descriptions. When you open the file, the computer uses the description to rebuild the original exactly.
Can compression make a file bigger?
Yes. If the data has almost no repeats, like a checkerboard or random noise, describing the runs takes more space than the data itself. That is why already compressed files barely shrink if you zip them again.