← Years 9–10 Data & Information

Run-length encoding of a pixel image

Compress a black-and-white image by writing each row as counts of same-coloured pixels in a run, starting with white. Row 00000 111 00 becomes 5,3,2. Counts can rebuild the grid.

Understanding Run-length encoding of a pixel image in Years 9–10 Data & Information

What it does: There are two runs: 4 white and 6 black. Writing the lengths in order gives 4,6. Each new count is a run of the opposite colour to the one before it. This is why the starting colour must be agreed. The colours change at every pixel, so every run has length 1. The row is 1 white, 1 black, 1 white, 1 black. A single 4 would rebuild 0000. The first count of 3 is white (000) and the second count of 3 is black (111), so the row is 000111. Encoding each row separately lets you rebuild the grid one row at a time. Without row breaks you could not tell where one row ends.

Common mistakes: The row 0000011100 has 5 white, then 3 black, then 2 white pixels. The runs should be 5,3,2, not 5,2,3. The row starts with black pixels, but the first count always means white. It needs a 0 at the start, so the runs are 0,3,4.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 6–16) and word search (Modules 6–132).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
A row reads 0000011100 (0 = white, 1 = black). Which run-length code starts with white?
3,5,25,3,2
Matches
Row 1100 encoded as 2,2
↓
The row starts with black, but the first count always means white pixels. A 0 is needed first, so it should be 0,2,2.
Applying
Meanings
Term
run-length encoding
Meaning
Compress a black-and-white image by writing each row as counts of same-coloured pixels in a run, starting with white. Row 00000 111 00 becomes 5,3,2. Counts can rebuild the grid.
Practice
Start with white (0), then black (1)
Testing
Gaps
Runs: ,3,2
Check Answer
Spots
2Row: 0000011100
3Runs: 5,2,3

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Seal' badge — Smooth Formatter, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. A row reads 0000011100 (0 = white, 1 = black). Which run-length code starts with white?
A. 3,5,2
B. 5,3,2 ✓
There are 5 white pixels, then 3 black, then 2 white, so the row is 5,3,2.
2. A row reads 1100000. The code always starts with white. Which code is correct?
A. 0,2,5 ✓
B. 2,5
The row starts with black, so the first white run has 0 pixels. The code is 0,2,5. Starting with 0 keeps the white/black order correct.
3. Using white first (0 = white, 1 = black), what row do the counts 4,2,1 rebuild?
A. 0000110 ✓
B. 1111001
The first count is white: 4 white, 2 black, 1 white gives 0000110. Starting with black would flip the whole row.
4. How are the counts in a run-length code for a pixel row arranged?
A. They all count the black pixels only
B. They alternate between white runs and black runs ✓
Each new count is a run of the opposite colour to the one before it. This is why the starting colour must be agreed.
5. An all-black row of 4 pixels (1111) is encoded, starting with white. What is the code?
A. 0,4 ✓
B. 4
There are 0 white pixels first, then 4 black. Without the 0, 4 would be read as 4 white pixels.

Match the Pairs

5,3,2 → 5 white pixels, then 3 black, then 2 white: the row 0000011100
0,4,2 → The row starts with black: 0 white pixels, then 4 black, then 2 white (111100)
Encoding a row of 4 white pixels followed by 6 black pixels → 4,6
Rebuilding a row from the counts 2,1,3 (white first, then alternating, 0 = white, 1 = black) → 001000
Row 1100 encoded as 2,2 → The row starts with black, but the first count always means white pixels. A 0 is needed first, so it should be 0,2,2.
Row 0001100 encoded as 3,2 → The last run of 2 white pixels is missing. Every run must be counted, so it should be 3,2,2.

Key Term

run-length encoding
Compress a black-and-white image by writing each row as counts of same-coloured pixels in a run, starting with white. Row 00000 111 00 becomes 5,3,2. Counts can rebuild the grid.

Fill in the Blanks

1. Row: 0000011100 Runs: 5,3,2
Hint: Type the length of the first run of white pixels (0s) in the row.
2. Row: 1110000 Runs: 0,3,4
Hint: This row starts with black, so type the count of white pixels at the start.
3. Runs: 2,3,1 Row: 001110
Hint: Type the pixels that rebuild the black run of the middle count.
4. Row: 00110000 Runs: 2,2,4 Width: 8
Hint: Type the total number of pixels in the row, which is the sum of the counts.
5. Runs alternate: white, black, white
Hint: Type the colour of the second run in a row.

Spot the Error

Question 1
A Pixels: 0 = white, 1 = black
B Row: 0000011100
C Runs: 5,2,3
Line C — The row 0000011100 has 5 white, then 3 black, then 2 white pixels. The runs should be 5,3,2, not 5,2,3.
Question 2
A Row: 1110000
B Runs: 3,4
C Rebuild: 1110000
Line B — The row starts with black pixels, but the first count always means white. It needs a 0 at the start, so the runs are 0,3,4.
Question 3
A Image row 1: 00111
B Runs: 3,2
C Total pixels: 5
Line B — The row has 2 white pixels first, then 3 black pixels. The runs should be 2,3. The counts are in the wrong order.
Question 4
A Row: 000110
B Runs: 3,2,2
C Total: 3+2+2 = 7 pixels
Line A — The runs 3,2,2 add up to 7 pixels, so the row must have 7 pixels. The row should be 0001100. 000110 has only 6 pixels.
Question 5
A Rule: first run is black
B Row: 1100
C Runs: 0,2,2
Line A — The convention is that the first count is always the white run. That is why a row starting with black begins with 0, as in 0,2,2. The rule should say white.

Student with a class code? Join your class →