Understand how files are compressed using lossy and lossless compression methods
📦 Data Storage & Compression
In everyday life we pack our clothes into a suitcase to fit more into a small space. In computing we do the same with data compression – we squeeze files into a smaller size so they take up less space on a hard drive or move faster over the internet.
Why Compress?
- 💾 Save storage space – a 1 GB file can become 200 MB.
- 🚀 Speed up transfer – less data means faster downloads.
- 🔒 Reduce bandwidth costs – especially useful for streaming services.
Compression Types
- Lossless – every bit of the original data can be perfectly recovered.
- Lossy – some data is permanently removed to achieve higher compression.
Lossless Compression
Think of a ZIP file or a PNG image. If you unzip a ZIP or open a PNG, you get exactly the same file you started with.
Common algorithms:
- Huffman coding
- LZ77 / LZ78 (used in ZIP, GZIP)
- Run‑Length Encoding (RLE) – great for simple graphics.
Lossy Compression
Imagine a photo you take with your phone. The phone uses a lossy algorithm to drop some colour details that our eyes can’t notice, making the file much smaller.
Common algorithms:
- JPEG for images – removes high‑frequency details.
- MP3 / AAC for audio – discards sounds outside human hearing range.
- H.264 / H.265 for video – removes redundant frames and colour information.
Comparing Lossless & Lossy
| Feature | Lossless | Lossy |
|---|---|---|
| Data Integrity | Exact recovery | Approximate recovery |
| Typical Use | Text, code, medical images | Photos, music, video |
| Compression Ratio | ~2–4× | ~10–100× |
Practical Example: JPEG Compression
1️⃣ Divide the image into 8×8 blocks.
2️⃣ Apply Discrete Cosine Transform (DCT). This converts pixel values into frequency components.
3️⃣ Quantise. High‑frequency components are rounded to zero – this is where data loss occurs.
4️⃣ Encode. Use Huffman coding to store the remaining numbers efficiently.
The result is a file that looks almost identical to the original but is much smaller.
Hands‑On Activity
- Take a photo with your phone and save it as JPEG.
- Open the same photo in a text editor that shows file size (e.g., Notepad++).
- Compare the file size to the original RAW or PNG version.
- Notice how the image quality changes when you adjust the JPEG compression level.
Key Takeaways
- Compression reduces file size by removing redundancy.
- Lossless keeps every bit – essential for data that must not change.
- Lossy sacrifices some detail for a huge size reduction – suitable for media.
- Choosing the right method depends on the file type and how it will be used.
Remember: just like you wouldn’t want to lose your favourite song, you also don’t want to lose important data. Pick the right compression tool for the job! 🎧📚
Revision
Log in to practice.