How Image Compression Actually Works
What happens when you drag the quality slider, how DCT transforms photos, and why screenshots compress differently.
Why does the same photo weigh 3 MB as a PNG but only 300 KB as a JPG? And why do modern browsers keep pushing WebP? The answer comes down to how each format compresses pixel data — and the trade-offs it makes along the way.
Every image format falls into one of two camps. Lossy formats (JPG, WebP lossy) throw away visual information that humans are unlikely to notice. The file gets dramatically smaller, but you can never recover the discarded data. Lossless formats (PNG, WebP lossless, GIF) compress without losing a single pixel — what you put in is exactly what you get out.
PNG uses a lossless compression algorithm called DEFLATE (the same one behind ZIP files). Every pixel is preserved exactly, which makes PNG ideal for screenshots, UI mockups, and images with large flat-color areas.
The downside? PNG files are large for photographs. A 12 MP photo can easily be 10+ MB as a PNG because complex color gradients don't compress well with DEFLATE.
JPG (also called JPEG) has dominated photo compression since 1992. It works by applying a Discrete Cosine Transform (DCT) to split the image into frequency components, then aggressively discarding high-frequency detail that the human eye is less sensitive to.
At quality 80–85, JPG typically achieves a 10:1 compression ratio with virtually no visible quality loss on photographs. Drop to quality 60 and you get even smaller files, but may notice softness around sharp edges.
Developed by Google, WebP supports both lossy and lossless compression, plus transparency and animation — all in one format. Lossy WebP is typically 25–35% smaller than equivalent-quality JPG, while lossless WebP beats PNG by around 26%.
WebP is supported in all modern browsers (Chrome, Firefox, Safari, Edge). The only reason not to use it is legacy software compatibility — some older image editors and email clients still don't handle WebP.
AVIF, based on the AV1 video codec, pushes compression even further — roughly 50% smaller than JPG at equivalent quality. Browser support is growing fast, but encoding is significantly slower than WebP, making it better for pre-processed assets than real-time conversion.
Here's how the major formats stack up:
| Format | Compression | Transparency | Animation | Best For |
|---|---|---|---|---|
| PNG | Lossless | Yes (alpha) | No | Screenshots, logos, UI |
| JPG | Lossy | No | No | Photos, gradients |
| WebP | Both | Yes (alpha) | Yes | Web images (all types) |
| GIF | Lossless | 1-bit only | Yes | Simple animations |
| AVIF | Both | Yes (alpha) | Yes | Next-gen web delivery |
The same 1920×1080 photograph saved in different formats shows dramatic size differences:
Same photo (1920x1080, 24-bit color):
PNG (lossless) → 4,200 KB
JPG (quality 85) → 380 KB
WebP (quality 85) → 270 KB
AVIF (quality 85) → 190 KB
Same screenshot (1920x1080, flat colors):
PNG (lossless) → 120 KB
JPG (quality 85) → 210 KB ← actually bigger!
WebP (lossless) → 88 KBNotice the reversal with screenshots: JPG is larger than PNG for flat-color images because DCT compression is designed for continuous-tone photographs, not sharp edges and solid blocks.
The best image format is the one that delivers the smallest file at acceptable visual quality for your specific content. There is no universal winner — only the right tool for the job.
What happens when you drag the quality slider, how DCT transforms photos, and why screenshots compress differently.
How QR encodes data in binary modules, why error correction lets you put logos in the center, and what those corner squares do.
Grid vs freeform composition, how canvas APIs stitch images together, and DPI considerations when combining images.