Lossless vs Lossy Compression
Lossless compression restores data exactly; lossy compression trades fidelity for much smaller size.
The distinction
Lossless compression guarantees the decompressed output is bit-for-bit identical to the input. Lossy compression permanently discards some information, so the reconstruction is only an approximation — but often a much smaller one.
When lossless is required
Text, source code, executables, spreadsheets, and archives must be lossless: a single changed bit can corrupt meaning. Formats like ZIP, gzip, PNG, and FLAC preserve every bit.
When lossy is acceptable
Photos, audio, and video tolerate loss because human perception is limited. JPEG, MP3, AAC, and modern video codecs remove detail the senses barely notice, reaching ratios far beyond any lossless method.
How lossy achieves more
Lossy codecs transform data into a domain (such as frequency) where importance is uneven, then quantize the least perceptible components coarsely or drop them. The remaining data is entropy-coded losslessly.
One-way street
Lossy compression cannot be undone; re-encoding an already-lossy file compounds the damage, a effect called generation loss. Keep a lossless master and derive lossy copies from it rather than from each other.
Choosing
Match the method to the data and the tolerance for error. If exact recovery matters at all, lossless is the only safe choice; if size dominates and small perceptual changes are fine, lossy wins decisively.
# Lossless round-trips exactly; lossy does not
import zlib
d = b'exact data'
print(zlib.decompress(zlib.compress(d)) == d) # True