Lossy and lossless compression
▶ Watch the video lessonShort, exam-focused explanation of this topic on the CS with Aamir YouTube channel.Compression reduces the size of a file. You need to know why we compress files and the difference between the two types of compression: lossy and lossless.
Why compress files?
- Files take up less storage space.
- Files are quicker to send over a network or the internet, so web pages load faster and videos stream smoothly.
- Less bandwidth is used, which can reduce costs.
Lossy compression
Lossy compression permanently removes some data from the file. The original file cannot be recreated exactly. It is used where a small drop in quality is not noticed by people.
- Images: reduce the resolution or the colour depth, or remove detail the eye is unlikely to see (e.g. JPEG).
- Sound: reduce the sample rate or sample resolution, or remove sounds the human ear cannot hear, such as very high or very low frequencies and quiet sounds played at the same time as louder ones (e.g. MP3).
Lossy compression gives a much smaller file, but it is not suitable for text documents or program files, because losing any data would change their meaning or stop them working.
Lossless compression
Lossless compression makes the file smaller without losing any data. The original file can be recreated exactly when it is decompressed. It is used for text, program files and anything that must not change (e.g. ZIP files, PNG images).
Run-length encoding (RLE)
RLE is a lossless method. It replaces a run of repeated values with a single copy of the value and a count of how many times it repeats.
The original uses 10 characters; the compressed version stores just 4 pairs.
RLE works well on data with long runs of the same value, such as simple images with large areas of one colour. On data with few repeats it can even make the file bigger, because every single value needs a count as well.
Lossy or lossless? A quick comparison
| Lossy | Lossless | |
|---|---|---|
| Data removed? | Yes, permanently | No |
| Original can be restored? | No | Yes, exactly |
| File size reduction | Large | Smaller |
| Typical use | Photos, music, video for the web | Text, programs, spreadsheets |
Exam tips
- When asked which type to use, justify your choice: e.g. "lossless, because the text must be identical when decompressed".
- Give specific methods for lossy compression (reducing resolution, colour depth, sample rate), not just "data is removed".
- Be ready to compress or decompress a short string or image row using RLE.