AP Computer Science Principles · Unit 2 lecture · Part 7 of 11 · 7 min

2.2 Data compression: lossySmaller still, if you can live with "almost"

Mr. Thompkins
Here's a trade: I'll make your photo ten times smaller, but I get to throw away a little detail you probably won't notice. Do you take it? For a beach photo, sure. For your bank records, never.

Lossy compression algorithms can significantly reduce the number of bits stored or transmitted, but they only allow the reconstruction of an approximation of the original data. Some information is thrown away permanently. The trick is choosing what to throw away: details people barely notice, like tiny color differences in a photo or sounds too quiet to hear under louder ones.

Here's a simple picture of the idea. Round each pixel's brightness to the nearest 10. Eight pixels that had seven different values now use only three: 50, 60 and 70. Fewer distinct values means more repetition, which compresses much better. But you can never get the original values back. Was that first pixel 52 or 49? The rounded file can't say.

123456784050607080pixelbrightnessoriginalrounded to tens
Lossy idea: rounding brightness to tens. The red copy is close, uses only 3 values instead of 7, and can never be turned back into the blue original.
?Wait, what's the difference between lossless and lossy again?
Lossless: smaller, and the original comes back exactly. Lossy: usually much smaller, but only an approximation comes back; some data is gone for good.

So which do you choose? The course gives a clean rule. In situations where quality, or the ability to reconstruct the original exactly, is maximally important, use lossless compression. In situations where minimizing size or transmission time is maximally important, use lossy compression. Video streaming, music apps and photo sharing usually use lossy. Documents, spreadsheets, program code and medical records use lossless.

SituationChooseWhy
A spreadsheet of bank balancesLosslessEvery digit must come back exactly
Source code of a programLosslessOne changed character can break it
A video stream on a slow connectionLossySmall size matters most; slight blur is fine
Photos for a social media postLossyFaster upload, the eye barely notices
A medical scan a doctor will studyLosslessLost detail could hide something important
Choosing a compression type: ask what matters most, exactness or size.
?Wait, does lossy compression make a file "worse"?
It makes it less exact, but often not noticeably worse. Lossy formats are designed around human senses, so they remove what we're least likely to notice. Compress too hard, though, and you start to see blocky colors or hear a "swishy" sound.
Pause and think
A file is compressed with lossy compression, then decompressed, then compressed again with lossless compression. Can the original ever be recovered?
I thought about it. Show me
No. The lossy step already threw information away permanently. The lossless step can perfectly restore what it was given, which is the approximation, not the original.
Choose and justify
For each situation, choose lossless or lossy and justify your choice in one sentence. (a) A teacher emails a spreadsheet of final grades. (b) A video call app on a weak connection. (c) A photographer archives original photos to edit and print later. (d) A podcast app that lets listeners download 100 episodes onto a phone with little space.
  1. (a) Lossless: every grade must come back exactly, and a changed digit would be a real error.
    Why?
    Exactness is maximally important.
  2. (b) Lossy: keeping the call smooth on little bandwidth matters more than perfect detail in each frame.
    Why?
    Minimizing transmission time is maximally important.
  3. (c) Lossless: edits and large prints need all the original detail, and lossy loss can't be undone later.
  4. (d) Lossy: speech still sounds clear after lossy compression, and fitting more episodes matters most.

Answer: (a) Lossless. (b) Lossy. (c) Lossless. (d) Lossy. In each case, decide whether exactness or size matters most.

Stuck? Get a hint
  1. For each one, ask: would losing a little detail cause a real problem?
  2. If yes, lossless. If small size or speed matters most, lossy.
  3. Remember lossy loss can never be undone.
Check yourself
A hospital stores X-ray images that doctors will examine for tiny details, sometimes years later. Which compression is most appropriate, and why?
  1. ALossy, because medical files are large
  2. BLossless, because the images must be reconstructed exactly
  3. CLossy, because doctors can request a new X-ray if needed
  4. DNo compression can be used on images
Show the answer

B. When the ability to reconstruct the original is maximally important, lossless is the right choice. Large size is a real concern, but lossy compression could remove exactly the tiny detail a doctor needs.

Check yourself
Which statement correctly compares lossy and lossless compression?
  1. ALossy compression usually produces a smaller file, but the original cannot be reconstructed exactly.
  2. BLossless compression always produces a smaller file than lossy compression of the same data.
  3. CLossy compression can be undone exactly if the file is decompressed twice.
  4. DLossless compression removes details people cannot notice.
Show the answer

A. Lossy trades exactness for size: much smaller files, approximate reconstruction. Lossless files are usually bigger than lossy ones, and lossy loss can never be undone.

Exam tip from Mr. T

Compression questions almost always hinge on one phrase in the stem. "Must be restored exactly", "every digit", "text", "code": lossless. "As small as possible", "fast streaming", "slight loss is acceptable": lossy.

  • Lossy: can greatly reduce bits, but only an approximation of the original can be reconstructed.
  • Use lossless when quality or exact reconstruction is maximally important.
  • Use lossy when minimizing size or transmission time is maximally important.
  • Information removed by lossy compression is gone permanently.