Data compression reduces file sizes so that files take less storage space and transfer more quickly across networks. There are two fundamentally different approaches: lossless compression, which allows the exact original data to be perfectly reconstructed, and lossy compression, which permanently discards some data to achieve far greater size reductions.
What is lossless compression and how does it work?
Lossless compression encodes data more efficiently without losing a single bit. When you decompress the file, you recover the original data exactly — identical byte for byte.
It achieves this by finding and eliminating redundancy — patterns that repeat and can be described more concisely. Two widely used lossless techniques are:
-
Run-length encoding (RLE): Replaces sequences of repeated values with a count and a value. For example, the sequence
AAAAAABBBbecomes6A3B. If the original was0000000000 0000111100 0001111111(a row of a black-and-white image), RLE stores14 white, 4 black, 2 white, 7 black, 5 white— far fewer values. -
Huffman coding: Assigns shorter bit patterns to frequently occurring symbols and longer bit patterns to rare ones. In English text, the letter
egets a very short code;zgets a longer one. Overall, the average bits per character drops well below 8.
What is lossy compression and how does it work?
Lossy compression throws away information that is unlikely to be noticed by humans, achieving far higher compression ratios than lossless methods.
JPEG image compression works by:
- Converting the image from RGB colour to a luminance-chrominance model (human eyes are more sensitive to brightness than to colour).
- Dividing the image into 8×8 pixel blocks.
- Applying a mathematical transform (Discrete Cosine Transform) to each block.
- Discarding the high-frequency components that represent fine detail (less visible to the eye).
- Compressing the remaining coefficients with a lossless step.
A JPEG quality setting of 80% discards far more detail than 95% — the trade-off is clearly visible when quality is set very low (blockiness and blurring at edges).
MP3 audio compression exploits psychoacoustics — the science of how humans perceive sound. Sounds masked by louder simultaneous sounds, and sounds outside the audible frequency range (above ~20 kHz), are discarded. The remainder is encoded efficiently. A typical MP3 at 128 kbps uses roughly 9 times less storage than the equivalent uncompressed WAV or FLAC file.
How do lossless and lossy compression compare?
| Aspect | Lossless | Lossy |
|---|---|---|
| Data recovery | Perfect — original restored exactly | Impossible — lost data is gone |
| Compression ratio | Moderate (typically 2:1 to 5:1) | High (10:1 to 100:1 or more) |
| File size | Larger than lossy | Much smaller |
| Quality | Identical to original | Degrades with lower quality settings |
| Use cases | Text, code, medical images, databases | Photographs, music, video streaming |
| Common image formats | PNG, GIF, TIFF, BMP | JPEG, WebP (lossy mode) |
| Common audio formats | FLAC, WAV, AIFF | MP3, AAC, OGG Vorbis |
| Common video formats | Lossless H.264, HuffYUV | H.264/AVC, H.265/HEVC, AV1 |
When should you use each type?
Use lossless when:
- The data must survive repeated edit/save cycles without degrading (source code, spreadsheets, ZIP archives).
- Every detail is medically or legally significant (X-rays, MRI scans, digital evidence).
- The file will be further edited and re-saved — each save of a JPEG re-applies compression, degrading quality cumulatively.
Use lossy when:
- Humans are the end audience and cannot perceive the discarded detail at a reasonable quality setting.
- Storage space or bandwidth is limited (streaming music, video over mobile networks).
- The file is a final delivery version, not an editable source.
Does lossy compression affect every format it applies to equally?
No. The perceptual impact depends on the codec (encoder/decoder), the quality setting, and the content:
- Photographs with smooth colour gradients survive JPEG compression well; images with sharp edges, text, or solid blocks of colour show artefacts much more visibly — this is why screenshots are better saved as PNG.
- Classical music with quiet passages is more affected by aggressive MP3 compression than a loud rock track.
Frequently asked questions
What is the difference between lossless and lossy compression in one sentence?
Lossless compression allows the original data to be perfectly recovered; lossy compression permanently removes some data to achieve a smaller file size.
Why are medical images always stored in lossless formats?
In medical imaging (X-rays, MRIs, CT scans), every pixel of detail can be clinically significant. A radiologist might diagnose a tumour from a subtle density variation that lossy compression would discard as imperceptible noise. Losing that data could contribute to a misdiagnosis — which is why medical imaging standards (such as DICOM) mandate lossless storage.
Can you convert a lossy file back to lossless quality?
No. Converting a JPEG to PNG does not restore the discarded data — it merely packages the already-degraded pixel values in a lossless container. The information lost during JPEG compression is permanently gone. To preserve full quality, you must start with the original uncompressed or lossless source.
Why does re-saving a JPEG reduce quality?
Each time you open a JPEG, edit it, and save it again, the lossy compression algorithm runs again on the already-compressed image. Each generation discards a little more data. After several such cycles, visible artefacts accumulate noticeably — a phenomenon called "generation loss". This is why professional photographers keep original RAW or TIFF (lossless) master files and only export JPEG for final delivery.
Professor Turing at aitutors.me can walk you through compression algorithms with diagrams and practice questions tailored to your exact exam board.