Every photo you see on the web is compressed. Without compression, a single 12-megapixel photo would be 36 megabytes — one photo per email attachment limit. Compression shrinks that to 2-5MB (or less) while keeping it visually nearly identical. How? Let's look under the hood.
| Lossless | Lossy | |
|---|---|---|
| How it works | Finds exact patterns, encodes them more efficiently | Discards data human eyes can't easily see |
| Quality | 100% identical to original | Visually near-identical, not pixel-perfect |
| File size | Moderate reduction (20-50%) | Dramatic reduction (70-95%) |
| Formats | PNG, GIF, WebP (lossless mode) | JPEG, WebP (lossy mode), AVIF |
| Best for | Logos, text, icons, screenshots | Photos, gradients, complex images |
Think of lossless compression like shorthand. Instead of writing "blue pixel, blue pixel, blue pixel, blue pixel" (4 data points), you write "4× blue pixel" (2 data points). This is called run-length encoding, and it's the simplest form of lossless compression.
PNG uses a more sophisticated algorithm called DEFLATE, which combines two techniques:
The result: PNG files are typically 20-40% smaller than uncompressed, and every single pixel is mathematically identical to the original.
Lossy compression goes further by exploiting human visual perception. Your eyes are more sensitive to brightness changes than color changes, and more sensitive to large-scale patterns than fine texture. JPEG exploits this brilliantly:
JPEG converts RGB to YCbCr (luminance + two color channels), then typically downsamples the color channels by 2x or 4x. Your eye barely notices because brightness carries most of the detail perception.
The image is divided into 8×8 pixel blocks. Each block is transformed from the spatial domain (pixel positions) to the frequency domain using DCT — the same math behind MP3 audio compression. The result is a matrix where the top-left values represent broad color changes and the bottom-right represents fine texture.
The frequency values are divided by a quantization table and rounded to integers. High-frequency (fine detail) values often round to zero. This is the only lossy step — and the quality slider in every JPEG compressor controls exactly how aggressively this division happens.
The resulting sparse matrix (lots of zeros) is then compressed losslessly using Huffman coding, similar to PNG's final step.
JPEG's DCT-based approach was invented in 1992. Modern formats use techniques from video codecs:
The result: WebP images are 25-35% smaller than equivalent-quality JPEGs. AVIF images are 50% smaller. The tradeoff is encoding speed — WebP takes 2-3x longer to encode than JPEG, and AVIF takes 5-10x longer.
For websites, use WebP or AVIF — they produce dramatically smaller files at equivalent quality. For sharing and archiving, JPEG at 80-90% quality remains the universal standard. For screenshots and images with text, use PNG (lossless). The "right" format depends entirely on what you're optimizing for: file size, compatibility, or pixel-perfect accuracy.