← CS Unplugged

CS Unplugged activity

Pixel Pictures: how it works

Part 1 of 3. Read this sheet, then do Part 2: Decode & Encode and Part 3: Make Your Own.

Every picture on a screen, every photo, every icon, every letter you’re reading right now, is made of tiny colored squares called pixels (short for “picture elements”). Zoom in far enough on any image and that’s all you’ll find: a grid of small, solid squares, nothing smoother underneath.

A computer can’t draw a soft curve by magic. It can only store numbers. So to store a picture, it needs a way to turn a grid of pixels into a list of numbers, and back again. This activity walks you through one simple way to do exactly that, for a picture that only uses black and white.

The rule

Look at one row of a black and white picture at a time, left to right. Instead of writing down the color of every single pixel, write down how many pixels you see before the color changes. Each one of those counts is called a run, and the list of counts for a row is its code.

The one rule that makes this work: the first number in a row’s code is always a count of white pixels. If a row actually starts with a black pixel, you still write the white count first, and it’s just 0.

For example, this five pixel row:

..###

has 2 white pixels, then 3 black. Its code is 2, 3.

This row also has five pixels, but starts black:

##...

has 0 white pixels (none, before the black starts), then 2 black, then 3 white. Its code is 0, 2, 3.

Add up the numbers in any row’s code and you should always get back the width of the picture (5, in both examples above). That’s a quick way to check your own work as you go: if a row’s numbers don’t add up to the grid’s width, you’ve made a mistake somewhere in that row.

Ready? Continue to Part 2: Decode & Encode.

Adapted from CS Unplugged, 2015, by Tim Bell, Ian H. Witten and Mike Fellows; adapted for classroom use by Robyn Adams and Jane McKenzie (csunplugged.org). This version by Eric Brown. Licensed under CC BY-NC-SA 3.0.