Skip to main content
B
Benrio
← Back to guides
Media8 min readMay 2026

Image Compression Explained: Formats, Quality, and File Size

Learn how lossy and lossless compression work, when to use JPEG vs. PNG vs. WebP, how quality settings affect file size, and best practices for optimizing images on the web.

Why Image Compression Matters

Images account for nearly half of the average web page's total weight. A single uncompressed photograph can easily exceed 5 MB, while the same image compressed to WebP at 80% quality might come in under 200 KB — a 96% reduction. That difference translates directly into faster page loads, lower bandwidth costs, and better Core Web Vitals scores. For mobile users on limited data plans, efficient compression isn't a nice-to-have — it's essential.

Lossy vs. Lossless Compression

Image compression falls into two categories. Lossy compression permanently discards visual information the human eye is unlikely to notice. It exploits how our vision is more sensitive to brightness changes than color shifts, and more attuned to smooth gradients than fine high-frequency detail. By strategically removing this less-perceptible data, lossy formats achieve dramatic size reductions.

Lossless compression preserves every single pixel exactly. It uses techniques like run-length encoding and dictionary-based algorithms to represent repeated patterns more efficiently. The tradeoff is smaller savings — typically 10–40% reduction compared to 60–90% with lossy methods. Use lossless when you need pixel-perfect fidelity: screenshots with text, technical diagrams, or archival copies.

Understanding JPEG

JPEG (Joint Photographic Experts Group) has been the web's workhorse image format since the 1990s. It uses a process called Discrete Cosine Transform (DCT) to convert spatial pixel data into frequency components. High-frequency details (sharp edges, noise) are then quantized more aggressively based on your quality setting. At quality 80, most photographs look indistinguishable from the original. Below 50, blocking artifacts — the telltale 8×8 pixel grid pattern — become visible.

JPEG works best for photographs and images with smooth color transitions. It struggles with sharp edges, text overlays, and flat-color graphics, where the DCT process creates visible ringing artifacts. It also doesn't support transparency — any transparent regions become solid white or black.

Understanding PNG

PNG (Portable Network Graphics) uses lossless compression based on the DEFLATE algorithm — the same one used in ZIP files. It predicts each pixel's value from its neighbors (using one of five filter methods), then compresses the prediction errors. Because no data is discarded, PNG files are always larger than their lossy equivalents for photographic content.

Where PNG excels is images with large areas of identical color, sharp edges, and transparency. Logos, icons, screenshots, and UI elements are all ideal PNG candidates. PNG supports full alpha-channel transparency (256 levels of opacity per pixel), making it the standard for images that need to composite cleanly over any background.

Understanding WebP

WebP, developed by Google and released in 2010, combines the best of both worlds. Its lossy mode uses a prediction-based approach derived from the VP8 video codec, typically achieving 25–35% better compression than JPEG at equivalent visual quality. Its lossless mode outperforms PNG by 20–25% on average.

WebP supports both lossy and lossless compression, alpha transparency, and even animation (replacing animated GIFs at a fraction of the file size). Browser support is now universal across Chrome, Firefox, Safari, and Edge. For new web projects, WebP is the recommended default format unless you need to support very old browsers or email clients.

How Quality Settings Work

The quality slider (typically 1–100) controls how aggressively the encoder discards information. It's not a linear scale — the relationship between quality number and file size follows a curve. Dropping from 100 to 80 might halve the file size with barely visible changes. Dropping from 80 to 60 saves another 30–40% with minor softening. Below 40, quality degrades rapidly.

The sweet spot for most web use is 70–85% quality. At this range, you get 5–10x compression over the uncompressed original with quality loss that's imperceptible at normal viewing distances. For retina displays, you can often drop quality lower (60–70%) because the high pixel density masks compression artifacts.

PNG has no quality slider because it's lossless — every pixel is preserved exactly. The only variable is the compression effort (how hard the encoder works to find patterns), which affects encoding speed but not output quality.

Resizing and Compression

The single most effective way to reduce image file size isn't the quality slider — it's reducing dimensions. A 4000×3000 pixel photo contains 12 million pixels. Resized to 1280×960 for web display, it has 1.2 million pixels — 90% fewer. The file size drops proportionally, often more dramatically than any quality adjustment.

A good rule of thumb: never serve an image wider than 2× the display size (to account for retina screens). If your layout shows an image at 640px wide, a 1280px source image is sufficient. Anything larger wastes bandwidth without any visual benefit to the user.

Choosing the Right Format

  • Photographs: WebP (lossy) at 75–85% quality. Fall back to JPEG if WebP isn't supported.
  • Screenshots and UI: WebP (lossless) or PNG. Sharp edges and text need lossless encoding to stay crisp.
  • Icons and logos: SVG when possible (infinitely scalable). PNG or WebP (lossless) when raster is needed.
  • Transparency needed: WebP or PNG. JPEG doesn't support transparency.
  • Animations: WebP or MP4 video. Animated GIFs are 5–10× larger than equivalent WebP animations.

Client-Side vs. Server-Side Compression

Server-side compression tools (like ImageMagick, Sharp, or Squoosh CLI) offer the most control and best compression ratios. They can use advanced techniques like multi-pass encoding and perceptual optimization that take more CPU time but produce smaller files.

Client-side compression (like our Image Compressor tool) processes images entirely in your browser using the Canvas API. Your images never leave your device, making it completely private. The tradeoff is that browser encoders are generally less optimized than dedicated tools — particularly for PNG, where the Canvas API's built-in encoder may produce larger files than specialized compressors.

Performance Best Practices

Beyond compression, several techniques further improve image delivery. Lazy loading (loading="lazy") defers off-screen images until the user scrolls near them. The <picture> element with srcset serves different sizes and formats to different devices. Content Delivery Networks (CDNs) like Cloudflare or Imgix can automatically compress and resize images on the fly based on the requesting device.

Always specify width and height attributes on <img> tags. This lets the browser reserve the correct space before the image loads, preventing layout shifts (CLS) that hurt user experience and search rankings.

Try it yourself:

Image Compressor