Skip to content
DevToolKit
deep-dives

Understanding Image Compression: Lossy vs Lossless Explained

A deep dive into how image compression works, the difference between lossy and lossless methods, and how to choose the right approach for your images.

DevToolKit Team 6 min read
Table of Contents

Why Image Compression Matters

Images account for roughly 50% of the average web page’s total weight, according to HTTP Archive data from 2026. A single unoptimized photograph can easily exceed 5MB, while the same image compressed properly might weigh 200KB or less — a 25x reduction. For websites, this translates directly into faster load times, lower bandwidth costs, improved Core Web Vitals scores, and better user experience on slow connections.

Understanding how image compression works helps you make informed decisions about quality settings, format selection, and optimization strategies. The fundamental distinction is between lossy and lossless compression.

Lossy Compression

Lossy compression reduces file size by permanently discarding information the algorithm determines to be less perceptually important. The original image data cannot be perfectly reconstructed from a lossy-compressed file. However, at reasonable quality settings, the visual differences between the original and compressed versions are imperceptible to most viewers.

How JPEG Compression Works

JPEG, the most widely used lossy format, compresses images through a multi-step process. First, it converts the image from RGB to YCbCr color space, separating luminance (brightness) from chrominance (color). Since human vision is more sensitive to brightness changes than color changes, JPEG can subsample the color channels — typically reducing their resolution by half in each dimension — with minimal visible impact.

Next, JPEG divides the image into 8x8 pixel blocks and applies a Discrete Cosine Transform (DCT) to each block. This converts spatial pixel data into frequency coefficients. High-frequency coefficients, which represent fine details and sharp transitions, are then quantized more aggressively — effectively rounding them to zero. The quantization step is where actual information loss occurs, and the quality slider in tools like our image compressor controls how aggressive this quantization is.

A quality setting of 80-85 in JPEG typically provides a good balance: visually identical to the original for most photographs, with file sizes 80-90% smaller than uncompressed. Below quality 60, block artifacts become increasingly visible, particularly around sharp edges and in areas of smooth gradients.

How WebP Lossy Compression Works

WebP uses a different approach based on VP8 video codec technology. Instead of 8x8 blocks, WebP uses variable-size prediction blocks (4x4 to 16x16 pixels). The encoder predicts each block from previously decoded blocks, then compresses only the difference between prediction and reality. This prediction-based approach typically produces 25-35% smaller files than JPEG at equivalent visual quality.

For converting between these formats, our PNG to JPG converter and JPG to WebP converter make the process simple.

Lossless Compression

Lossless compression reduces file size without discarding any information. The original image can be perfectly reconstructed from the compressed file, bit for bit. The tradeoff is that lossless compression produces larger files than lossy compression for photographic images.

How PNG Compression Works

PNG uses a two-stage lossless compression pipeline. First, it applies prediction filters to each row of pixels. Each pixel is predicted from its neighbors (left, above, upper-left, or upper-right), and only the difference between prediction and actual value is stored. For images with smooth gradients or large flat areas, these differences are small numbers clustered around zero — which compress very efficiently.

The filtered data is then compressed using the DEFLATE algorithm (the same algorithm used in ZIP files), which combines LZ77 pattern matching with Huffman coding. DEFLATE finds repeated byte sequences and replaces them with back-references, then encodes the most common values with shorter bit sequences.

PNG excels at images with sharp edges, flat colors, and text — scenarios where JPEG’s block-based lossy compression introduces visible artifacts. Screenshots, diagrams, logos, and UI mockups are all natural fits for PNG. You can convert images to PNG using our PNG to WebP converter for additional size savings when lossless quality is not strictly required.

Lossless WebP

WebP also supports lossless compression, which typically produces files 26% smaller than PNG according to Google’s benchmarks. WebP lossless uses a combination of transform coding, entropy coding, and a novel backward reference approach that can reference 2D areas rather than just linear byte sequences.

Choosing Between Lossy and Lossless

The choice depends on your image content and requirements:

  • Photographs and natural images: Use lossy compression (JPEG, WebP lossy, or AVIF). The organic texture and color variation in photographs mask compression artifacts effectively.
  • Screenshots and UI images: Use lossless compression (PNG or WebP lossless). Sharp edges between UI elements and text create artifacts under lossy compression.
  • Graphics with transparency: Use PNG for lossless transparency, or AVIF/WebP for lossy transparency. JPEG does not support alpha channels.
  • Simple graphics and logos: Use SVG when possible (vector format, infinitely scalable). Fall back to PNG for raster logos.
  • Images requiring repeated editing: Always save working files in a lossless format. Each lossy save introduces additional quality degradation (generation loss).

Quality Settings: Finding the Sweet Spot

When using lossy compression, the quality setting controls the tradeoff between file size and visual fidelity. The relationship is not linear — the first 20% of quality reduction might save 70% of the file size, while the last 20% might only save another 5%.

For most web use cases, these quality ranges provide good results:

  • JPEG: 75-85 for general web images, 85-95 for hero images and product photography
  • WebP: 75-85 (WebP’s quality scale is calibrated differently, so the same number does not mean the same thing as JPEG)
  • AVIF: 50-70 (AVIF’s quality scale is more compressed; quality 60 in AVIF roughly matches JPEG quality 80)

Our image compression tool lets you experiment with these settings interactively, with a real-time preview showing the compressed result alongside the original. All processing runs in your browser — your images never leave your device.

Advanced Compression Techniques

Beyond format selection and quality settings, several techniques can further reduce image file sizes:

Responsive Images

Serving appropriately sized images for each device prevents wasting bandwidth on pixels the user cannot see. A 4K image displayed in a 400-pixel-wide card wastes over 90% of its data. The HTML srcset attribute and <picture> element enable serving different image sizes to different devices.

Progressive Loading

JPEG supports progressive encoding, where the image loads in multiple passes — first a low-resolution preview, then progressively sharper detail. This improves perceived loading performance even though the total file size may be slightly larger than baseline JPEG.

Metadata Stripping

EXIF data, color profiles, and other metadata can add 10KB or more to an image. Stripping non-essential metadata is a lossless way to reduce file size. However, be careful with color profiles — removing an sRGB profile usually has no effect, but removing a Display P3 or Adobe RGB profile can cause color shifts.

Chroma Subsampling

JPEG’s 4:2:0 chroma subsampling (the default in most encoders) reduces color resolution by 75% with minimal visual impact on photographs. For images with fine color detail — red text on white background, for example — using 4:4:4 subsampling preserves color sharpness at the cost of larger files.

Compression and Web Performance

Image optimization is one of the highest-impact performance improvements for most websites. Google’s Core Web Vitals, which influence search rankings, are directly affected by image loading behavior:

  • Largest Contentful Paint (LCP): Often determined by the hero image or largest visible image. Reducing image size directly reduces LCP.
  • Cumulative Layout Shift (CLS): Using explicit width and height attributes on <img> elements prevents layout shifts when images load. This is a zero-cost improvement.
  • Time to First Byte (TTFB): CDN caching of optimized images reduces server response times for image requests.

For quick image optimization without installing any software, try our image compression tool. It runs entirely in your browser using WebAssembly for near-native performance, supports batch processing, and preserves your privacy by never uploading files to any server.

compression image-formats web-performance optimization