A two-hour HD movie contains about 100 gigabytes of raw video data. Netflix streams that movie to you at 5 gigabytes per hour — a 10-to-1 reduction. Your phone stores thousands of photos in the space that raw images would fill with dozens. Every file you download, every call you make over the internet, every song you stream relies on compression: the mathematics of representing the same information using fewer bits. How does compression work without losing anything — or losing only what you won't miss?

Lossless vs. Lossy Compression

There are two types of compression. Lossless compression reduces file size while preserving every bit of the original — decompress it and you get exactly the original back. ZIP files, PNG images, and FLAC audio use lossless compression. Lossy compression discards some information permanently — the decompressed file is an approximation of the original. MP3, JPEG, and the H.264 video codec are lossy. The art of lossy compression is discarding information you won't notice is missing.

Lossless: Huffman Coding

Every text file stores characters as fixed-length binary codes — typically 8 bits per character. But some characters appear far more often than others. In English, 'e' appears about 13% of the time; 'z' appears 0.07% of the time. Why use the same 8 bits for both? Huffman coding assigns shorter codes to frequent characters and longer codes to rare ones, reducing the average bits per character.

Fixed-length encoding: every character uses 8 bits Huffman encoding example: 'e' (13% frequency) → code: 10 (2 bits) 't' (9% frequency) → code: 01 (2 bits) 'z' (0.07%) → code: 111001010 (9 bits) Average bits per character ≈ 4.5 instead of 8 → text file reduced to 56% of original size The trick: frequent characters get short codes, rare ones get long codes. Average length decreases.

Huffman coding builds an optimal code by treating character frequencies as a binary tree, where the most frequent characters sit closest to the root (shortest path = shortest code). The algorithm is guaranteed to produce the most efficient fixed-tree code possible.

Lossy: JPEG Image Compression

JPEG exploits a fact about human vision: we're much more sensitive to brightness differences than to color differences, and we're much more sensitive to gradual changes than to rapid fine detail. JPEG first converts the image from color to a representation separating brightness from color, then applies the Discrete Cosine Transform (DCT) to break each 8×8 block of pixels into frequency components — like Fourier analysis but for images. Low-frequency components (gradual color changes) are kept at full precision. High-frequency components (fine detail) are heavily rounded, since the eye barely notices their loss.

JPEG pipeline for each 8×8 pixel block: 1. Apply DCT → 64 frequency coefficients (like asking: how much coarse pattern? how much fine detail?) 2. Divide each coefficient by a quality factor → round to integer (high-frequency coefficients get divided by larger numbers → more rounding) 3. Encode the rounded integers (with Huffman coding) At quality 80: typically 10:1 compression At quality 50: typically 20:1 compression, noticeable artifacts

Video: Exploiting Time

Video compression goes further by exploiting similarity between frames. In a typical scene, most of the frame is static — only small regions change between frames. H.264 (the codec behind most streaming video) encodes each frame as a set of changes from the previous frame, rather than as a full image. A slow-moving scene might encode 29 out of 30 frames as tiny "difference patches" rather than full images. Only when the scene cuts completely is a full frame needed. This inter-frame prediction is why a two-hour movie with 172,800 frames can be compressed to a fraction of what 172,800 full JPEG images would require.

Conclusion

Compression works by exploiting redundancy: common characters in text, gradual spatial variation in images, temporal similarity in video. Lossless methods like Huffman coding remove redundancy without discarding information. Lossy methods like JPEG go further, discarding information the receiver is unlikely to notice. Together, these mathematical techniques make streaming movies, storing thousands of photos on a phone, and sending files over the internet practical — the difference between a 100GB raw movie and a 5GB compressed one is the mathematics of information theory applied to human perception.