📦

Compression

Lossy & Lossless

What You'll Learn

🔮

Predict

Test your intuition about compression

📖

Worked Examples

Real-world compression scenarios

🔍

Trace

Analyse compression decisions

🧩

Parsons Problems

Put concepts in order

✅

Quiz

Test your knowledge

1. The Need for Compression

Why Do We Need Compression?

Digital files can be very large. Without compression, many everyday tasks would be impractical or impossible:

📧

Email Attachments

Most email services limit attachments to 25MB. An uncompressed photo could be 20MB+!

🎵

Music Streaming

An uncompressed 3-minute song is ~30MB. With compression (MP3), it's only ~3MB.

📱

Limited Storage

Phones have limited space. Compression lets you store thousands of photos instead of hundreds.

🌐

Web Pages

Compressed images load faster, saving bandwidth and improving user experience.

🎬

Video Streaming

A 2-hour 4K movie uncompressed would be ~1TB! Compression makes streaming possible.

💾

Backups

Compressing backups saves storage space and reduces backup time.

What is Compression?

Compression is the process of reducing the file size by encoding data more efficiently.

Key Idea: Compression finds patterns and redundancy in data and represents them more efficiently, using fewer bits to store the same (or similar) information.
📄

Original File

100 MB

→ Compress →
📦

Compressed File

25 MB

Compression reduces file size, making storage and transmission more efficient

Common Scenarios Requiring Compression

Scenario Why Compression Helps
Sending files via email Stay within attachment size limits
Uploading to cloud storage Faster uploads, use less storage quota
Streaming music/video Reduce bandwidth, enable smooth playback
Website images Faster page load times
Archiving old files Save storage space
Sending multiple files Bundle into one smaller file (ZIP)
Mobile data usage Reduce data consumption

2. Types of Compression

Two Main Types

Lossless Compression

"Shrink it, but keep everything"

The original data can be perfectly reconstructed when decompressed.

No data is lost.

Lossy Compression

"Shrink it more, lose some detail"

Some data is permanently removed to achieve smaller file sizes.

Some data is lost.

3. Lossless Compression

How Lossless Works (Simplified)

Lossless compression finds patterns and redundancy in data and encodes them more efficiently.

Simple Example: Run-Length Encoding

Original: AAAAABBBCCCCCCCC (16 characters)

Compressed: 5A3B8C (6 characters)

The pattern "5 A's, 3 B's, 8 C's" uses fewer characters but contains the same information!

Key Property: When you decompress a lossless file, you get back exactly the original data, bit for bit.

Common Lossless Formats

.ZIP

General file compression

.PNG

Images (graphics, screenshots)

.FLAC

Audio (music archival)

.GIF

Simple images, animations

.RAR

File archives

.7Z

High-ratio file compression

When to Use Lossless

📝

Text Documents

Every character matters - you can't lose any text!

💻

Program Files / Software

A single bit change could break the program

📊

Spreadsheets / Databases

Financial data, records - accuracy is critical

🎨

Graphics with Sharp Edges

Logos, diagrams, screenshots - PNG is ideal

🏥

Medical Images

Doctors need every detail for accurate diagnosis

Advantages & Disadvantages of Lossless

Advantages

  • No quality loss - original perfectly preserved
  • Can compress and decompress repeatedly
  • Essential for text, code, and critical data
  • Reversible - always get original back

Disadvantages

  • Lower compression ratio (files still larger)
  • Not as effective for photos/video
  • Files may still be too large for some uses

4. Lossy Compression

How Lossy Works (Simplified)

Lossy compression achieves smaller files by permanently removing data that humans are less likely to notice.

Examples of What Gets Removed

  • Images: Subtle colour variations, fine details
  • Audio: Frequencies humans can't hear well (very high/low)
  • Video: Details between similar frames
Important: Once data is removed by lossy compression, it's gone forever. You cannot get it back by "decompressing" - there's nothing to decompress!

Common Lossy Formats

.JPEG

Photos, web images

.MP3

Music, podcasts

.MP4

Video files

.AAC

Audio (iTunes, streaming)

.WEBP

Web images

.H.264

Video codec

Quality Levels

Lossy compression usually lets you choose a quality level - a trade-off between file size and quality.

Low Quality High Quality
Small file
Visible artifacts
Balanced
Good for web
Large file
Near-original
Smaller File Larger File

When to Use Lossy

📸

Photographs

JPEG compression removes details humans don't notice

🎵

Music for Everyday Listening

MP3 at 320kbps sounds nearly identical to CD

🎬

Video Streaming

Essential - uncompressed video would be impossibly large

🌐

Web Images

Fast loading is more important than perfect quality

📱

Mobile Apps

Smaller assets = faster downloads, less storage

Advantages & Disadvantages of Lossy

Advantages

  • Much smaller file sizes (often 90%+ reduction)
  • Makes streaming media practical
  • Faster uploads/downloads
  • Saves significant storage space
  • Quality loss often imperceptible

Disadvantages

  • Quality loss is permanent and irreversible
  • Re-compressing degrades quality further
  • Not suitable for text, code, or critical data
  • Visible/audible artifacts at high compression

Compression Artifacts

When lossy compression is too aggressive, you may notice artifacts - visible or audible flaws.

JPEG Artifacts (Images)

  • Blockiness: Visible squares, especially in gradients
  • Colour banding: Smooth gradients become stepped
  • Blurring: Loss of fine detail
  • Ringing: Halos around sharp edges

MP3 Artifacts (Audio)

  • Pre-echo: Ghost sounds before loud notes
  • Swirling: Metallic sound on cymbals
  • Muffled highs: High frequencies sound dull

5. Lossless vs Lossy: Side by Side

Complete Comparison

Feature Lossless Lossy
Data lost? No - original preserved perfectly Yes - some data permanently removed
File size reduction Moderate (typically 20-50%) High (typically 70-95%)
Quality Identical to original Reduced (may not be noticeable)
Reversible? Yes - fully reversible No - changes are permanent
Re-compression No quality loss Further quality loss each time
Best for Text, code, archives, graphics Photos, music, video, web
Common formats ZIP, PNG, FLAC, GIF JPEG, MP3, MP4, AAC

Decision Flowchart

What type of file?

Text, Code, Software, Data
↓
Use LOSSLESS

Every bit matters!

Photo, Music, Video
↓
Need perfect quality?
Yes (archival)
LOSSLESS
No (everyday use)
LOSSY

Quick Reference Card

Why Compress?

  • Save storage space
  • Faster file transfers
  • Stay within size limits
  • Enable streaming
  • Reduce bandwidth costs

Lossless

  • No data lost
  • Fully reversible
  • Smaller reduction
  • ZIP, PNG, FLAC
  • Use for: text, code, data

Lossy

  • Some data lost forever
  • Not reversible
  • Much smaller files
  • JPEG, MP3, MP4
  • Use for: photos, music, video

The Fundamental Trade-Off

Lossless: Keep everything, but files are still relatively large

Lossy: Much smaller files, but some quality is permanently lost

← Back to Topics Start Activities →