← Making data smaller

Lossy or lossless

Lossless compression gives back exactly the original data. Lossy compression throws away detail people are unlikely to notice, such as tiny differences in shade, so files get much smaller, but that detail is gone for good. Music and photos online are usually lossy; text, medical scans and evidence must be lossless.

A song streamed on your phone is about a tenth of the size of the same song on a CD. What did they throw away, and why can't you hear it?

Run-length encoding gives back exactly what went in: it is lossless. But a photo has few long runs, because neighbouring pixels are almost never exactly the same shade. Lossy compression makes runs longer on purpose: shades that are close enough (within a tolerance) are stored as the same. The file shrinks a lot, but the small differences are thrown away.

This board stores a grey picture as runs of count × shade, two numbers a run. Pixel by pixel it would be 48 numbers.

PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.

Does it come back?

A grey photo is compressed by treating nearly equal shades as the same, so the runs get longer. Then it is opened again. Is it exactly the same as the original?

TryTry it on the board and compare the result with your prediction.

Try: raise the tolerance

Was your prediction right? Slide the tolerance back to 0: the picture is perfect again, because the board always starts from the original. A saved lossy file doesn't have one.

Try it yourselfYour edits are saved in this browser.

Raise the tolerance to 40 or more. How many numbers are stored now, and how many pixels came back different?

Use the board. This ticks when you get there.

InvestigateWork out why it happens, one step at a time.

Worked example: lossless versus lossy

Worked exampleBefore each step, say what you think comes next.
  1. t=0:28 numbers,0 changedt = 0: 28 \text{ numbers}, 0 \text{ changed}Lossless: only exact runs. Already smaller than 48.

Now define lossless.

What makes compression **lossless**?

Sam explains what happens to a lossy photo. Click the line where it first goes wrong.

Which of these must be stored **lossless**?

ModifyChange the problem on purpose and see what happens.

Vary: one setting at a time

Try it yourselfYour edits are saved in this browser.

Set the tolerance to 0. How many pixels come back different?

Use the board. This ticks when you get there.

Push the tolerance to 128, the most. How many numbers now? Does it still look like the picture?

Use the board. This ticks when you get there.

From tolerance 40 to 128, the numbers stored drop from 16 to 12 but 20 pixels change instead of 16. Is the extra squeeze worth it?

A streaming service sends music in a lossy format. Why is that a good choice?

PractiseA few on your own, to check you have it.

Choose a format

Which format is **lossless**?

A police photo of a number plate is saved as a heavily compressed lossy file. What could go wrong?

You open a JPEG, edit it and save it as a JPEG again, many times. What happens?

Review: what is the run-length code for white, white, white, white, black, black, black, black?

MakeMake your own problem using what you have learned. Expect a few tries; that is normal.

The edge of lossless

Then: would that tolerance work for a different picture? Why not?

Try it yourselfYour edits are saved in this browser.
Need a hint?

Raise the tolerance one at a time and watch the pixels that came back different.

Another hint

The first change happens when two neighbouring shades are within the tolerance.

Another hint

Go back one from the first tolerance that changes a pixel.

Find the largest tolerance that still gives back every pixel exactly.

Use the board. This ticks when you get there.

For teachers

Students raise the tolerance of a lossy run-length compressor on a grey picture to see that lossy compression stores far fewer numbers but permanently changes data, and decide where lossy and lossless each belong (AC9TDI10K03 node 3). About 45 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

Try the Try and Make boards yourself.

The one idea: lossless gives back exactly the original; lossy discards information permanently to save more space; choose by purpose. Kit contexts for width: medical images and legal evidence (lossless), music streaming and photos online (lossy).

Simplification: real lossy formats (JPEG, MP3) don't merge nearby shades in runs; they use smarter maths to throw away what people perceive least. The board shows the same trade-off in a form students can see.

Running the lesson

  1. Predict: students who chose "yes" hold Sam's misconception; return to them after the Try.
  2. Try: ask which parts of the picture changed first, and why.
  3. Worked example: ask what a doctor or a court would say about the 16 changed pixels.
  4. Vary: debate the "worth it" question in pairs before checking.
  5. Make: compare: why is 27 the edge for this picture?

The hard part (on purpose)

Students believe lost detail can be recovered because the board can slide back to 0. Point out that the board keeps the original; a saved lossy file does not.

When students get stuck

Stretch and support

Stretch: Save a photo as JPEG at quality 100 and quality 10 and compare the sizes and what changed. Which details went first?

Support: Give the two definitions on a card: lossless = exact recovery; lossy = permanent loss for a smaller file.

Where it leads

Builds on: Run-length encoding. Where it leads: file formats and codecs (MP3 vs WAV, JPEG vs PNG), and the ethics of altered images and evidence.

What each step is for

Now you can
You reached the endBack to Making data smaller