Compression7 min read

Understanding Lossy vs Lossless Compression

Learn the fundamental difference between lossy and lossless compression, how each method works, and when to use them for optimal image quality and file size.

Published on October 22, 2024

What Is Image Compression?

Image compression reduces the size of an image file without sacrificing too much quality. It does this by finding patterns and redundant data in an image and storing that information more efficiently. Without it, even a standard 1080p photo could weigh in at more than 6 MB—far too heavy for a fast-loading website.

There are two main types of image compression—lossy and lossless. Knowing the difference between them helps you choose the right balance between speed, quality, and file size for every image on your site.


Lossless Compression: Flawless Quality, Bigger Files

Lossless compression shrinks image files without throwing away any data. When you decompress a lossless image, you get an exact replica of the original—every pixel and every bit is preserved. Think of it like zipping a file: once unzipped, nothing is missing or changed.

How Lossless Compression Works

Lossless algorithms work by spotting repetition and patterns in your image data. For example, if a sky is a single shade of blue, instead of saving “blue, blue, blue…” thousands of times, the algorithm simply notes “blue, 2,500 times.” This is called run-length encoding, but there are many more advanced methods at play.

Today’s lossless compression relies on a mix of clever techniques, such as:

  • Prediction and filtering: Predicting pixel values based on neighboring pixels and storing only the difference
  • Dictionary coding: Building a dictionary of commonly occurring patterns and replacing them with shorter codes
  • Entropy coding: Assigning shorter codes to frequently occurring values and longer codes to rare values

Formats That Use Lossless Compression

  • PNG: The go-to lossless format for web images
  • WebP (lossless mode): A newer option that usually outperforms PNG in file size
  • AVIF (lossless mode): State-of-the-art format, offering excellent compression
  • GIF: Classic format with lossless compression, but limited to 256 colors

Typical Compression Ratios for Lossless

Lossless compression usually cuts file sizes by about half or two-thirds, but results vary depending on the type of image:

  • Simple graphics/screenshots: 3:1 to 5:1 (huge savings thanks to big areas of solid color)
  • Photographs: 1.5:1 to 2:1 (less savings due to complex details)
  • Text and diagrams: 5:1 to 10:1 (extremely efficient—lots of repeated patterns)

When to Use Lossless Compression

Lossless compression is your best bet when:

  • Quality is paramount: Product images, medical imagery, archival photos, or any content where every detail matters
  • You have text or sharp lines: Logos, icons, screenshots, infographics, or diagrams where lossy artifacts would be very visible
  • You need transparency: PNG is the most compatible format for images with transparent backgrounds
  • Images will be edited repeatedly: Lossless formats don't degrade with multiple save cycles
  • File size is secondary: For logos and icons, the slight size penalty is worth perfect quality

Lossy Compression: Tiny Files, Some Quality Tradeoff

Lossy compression slashes file sizes by permanently removing some image information. The secret is to throw away details your viewers won’t notice—subtle textures or colors our eyes aren’t sensitive to. Done right, lossy compression can shrink images by 10 to 20 times while keeping them looking great.

How Lossy Compression Works

Lossy algorithms take advantage of how our eyes work. We’re more sensitive to shifts in brightness than in color, and we notice big shapes more than tiny details. Lossy compression leans into these quirks:

JPEG's Approach

  1. Color space conversion: Switches from RGB to YCbCr to separate lightness from color.
  2. Chroma subsampling: Reduces color information (which we notice less), but keeps brightness detail sharp.
  3. Frequency analysis: Splits the image into 8×8 blocks and analyzes patterns using the Discrete Cosine Transform (DCT).
  4. Quantization: Discards the fine-grained details our eyes usually miss, especially in texture and noise.
  5. Entropy coding: Uses lossless compression on the remaining data for extra savings.

JPEG’s quality setting controls how much detail gets tossed out during quantization. A higher setting keeps more detail (and results in a bigger file), while a lower setting dials up the compression but can introduce visible artifacts.

Formats That Use Lossy Compression

  • JPEG: The classic lossy format, perfect for photos
  • WebP (lossy mode): Delivers 25–35% smaller files than JPEG at similar quality
  • AVIF (lossy mode): Can shrink files by 40–50% more than JPEG, with impressive quality

Typical Compression Ratios for Lossy

Lossy compression can shrink images far more than lossless:

  • High quality (JPEG 85–95): 5:1 to 10:1 reduction
  • Medium quality (JPEG 75–85): 10:1 to 20:1 reduction
  • Low quality (JPEG 60–75): 20:1 to 40:1 reduction

For most web photos, a quality setting between 75 and 85 strikes a great balance—delivering 10–15x smaller files with little visible difference.

Common Lossy Compression Artifacts

Recognizing the side effects of lossy compression helps you avoid pushing file sizes too low:

1. Blocking Artifacts

JPEG divides images into 8×8 blocks, and when compression is heavy, these blocks can show up as a visible grid—especially in smooth areas or gradients.

2. Ringing and Halos

Strong edges (such as text on a background) can develop “ringing” or halo effects—wavy patterns around the edge. That’s why JPEG isn’t ideal for screenshots or images with lots of text.

3. Color Bleeding

Reducing color detail (chroma subsampling) can cause colors to blur into each other, making sharp color transitions look fuzzy. This is most obvious with red text on white.

4. Loss of Fine Detail

Heavy compression can blur away fine textures—think hair, grass, or fabric—leaving images looking soft or “smeared.”

When to Use Lossy Compression

Lossy compression is ideal when:

  • Working with photographs: Photographs benefit enormously from lossy compression and tolerate quality loss well
  • File size is critical: For bandwidth-constrained scenarios or pages with many images
  • Images won't be edited further: Lossy compression is a one-way operation; save originals separately
  • Slight quality loss is acceptable: For most web images, viewers won't notice appropriate lossy compression

Finding the Sweet Spot: Quality vs. File Size

The big question with lossy compression: “What quality setting should I pick?” The answer depends on your needs, but these research-backed guidelines will help you decide:

Quality Guidelines by Use Case

Image TypeRecommended QualityFormatNotes
Hero images80-85JPEG/WebP/AVIFHigh visibility, prioritize quality
Product photos80-90JPEG/WebPCritical for sales, maintain detail
Content images75-80JPEG/WebPBlog posts, articles, general use
Thumbnails70-75JPEG/WebPSmall size hides artifacts
Background images70-75JPEG/WebPLess scrutinized, prioritize file size
Logos/iconsLosslessPNG/SVGNever use lossy for graphics

The 80–85 Rule

Studies show that JPEG quality levels of 80–85 are the “sweet spot” for most web images:

  • Most people can’t tell the difference between quality 85 and quality 100
  • Quality 80 images are about 50–60% smaller than quality 95
  • Quality 85 cuts file size by 40–50% with almost no visible loss
  • Going below 75 can introduce obvious artifacts, especially on big screens

Hybrid Approach: Using Both Methods

The smartest workflow often blends both lossy and lossless compression:

1. Modern Formats with Dual Modes

WebP and AVIF can do both lossy and lossless compression. Use lossy for photos, lossless for graphics—all in one format.

2. Lossless Optimization of Lossy Files

Even after lossy compression, you can squeeze files smaller using lossless tools. For example, MozJPEG can shrink JPEGs by another 10–20% by optimizing how the data is stored and stripping unneeded metadata.

3. Format Selection Based on Content

Pick the format and compression method that matches the image type:

  • Photographs → Lossy (JPEG/WebP/AVIF)
  • Graphics/logos → Lossless (PNG/WebP lossless/SVG)
  • Screenshots with text → Lossless (PNG)
  • Photos with transparency → Lossy with alpha (WebP)

Advanced Compression Techniques

Perceptual Encoding

Cutting-edge compression tools use perceptual metrics (like SSIM and Butteraugli) to optimize for what people actually see—not just pixel math. By tuning compression based on human vision, these tools can deliver 15–25% smaller files at the same perceived quality compared to older methods.

Content-Aware Compression

Some tools are smart enough to treat different parts of an image differently. Faces, text, and sharp edges get higher quality, while backgrounds or blurry areas are compressed more aggressively. This approach maximizes savings without sacrificing important details.

Chroma Subsampling Options

JPEG allows for different levels of color data reduction (chroma subsampling):

  • 4:4:4: No color reduction (biggest files, sharpest colors)
  • 4:2:2: Reduces color horizontally (good compromise)
  • 4:2:0: Reduces color both horizontally and vertically (smallest files, standard for the web)

Most images online use 4:2:0, which keeps file sizes low without noticeably affecting photo quality.


Testing and Measuring Compression Quality

Don’t just trust your eyes—objective tools help you measure compression quality accurately:

Metrics to Track

  • File size: The simplest measure—smaller is usually better, but don’t go too far
  • PSNR (Peak Signal-to-Noise Ratio): A mathematical score; higher is better, but not always aligned with what people see
  • SSIM (Structural Similarity Index): Closer to 1.0 is better; this metric matches human perception more closely
  • Butteraugli: Google’s perceptual metric; lower numbers mean better visual similarity

A/B Testing

For important images (such as product photos), try A/B testing different quality settings to see how they affect conversions. Sometimes, a 20% smaller file that loads faster can boost sales, even if the image is technically a bit lower quality.


Practical Workflow Recommendations

For Website Owners

  1. Always keep originals in a lossless format (like PNG or lossless WebP) for future editing or re-exporting.
  2. Export photos at quality 80–85 in JPEG or WebP—this gives you great quality at a fraction of the size.
  3. Store graphics and logos in lossless formats (PNG or WebP lossless) to preserve sharpness.
  4. Use optimization tools (such as Squish Image Optimizer) to automate the right settings for every image.
  5. Use responsive images and adjust quality for each size to keep your site fast on every device.

For Developers

  1. Automate image compression in your build pipeline with tools like sharp or imagemin.
  2. Serve modern formats (WebP/AVIF) with fallbacks for older browsers.
  3. Use quality tiers for different image types and scenarios.
  4. Monitor file sizes and add CI/CD checks to prevent oversized images from sneaking into production.

Conclusion

Mastering the difference between lossy and lossless compression is the foundation of smart image optimization. Use lossless for graphics, logos, and anything that needs pixel-perfect clarity. Turn to lossy for photographs, where a little invisible quality loss can mean massive file size savings.

The trick is to match the right method to your image and find that sweet spot between quality and speed. For most web photos, lossy compression at quality 80–85 is ideal—delivering dramatic size reductions with no obvious drop in appearance. For graphics, stick to lossless formats like PNG or WebP lossless for crisp, clean results.

Modern formats like WebP and AVIF give you the flexibility to use both approaches and achieve even better compression. By understanding and applying these techniques, you'll keep your site fast and your images looking their best.


Sources and References

This article references technical documentation and research on image compression:

  1. MDN Web Docs (2024). "Image file type and format guide." Technical reference for image compression methods. developer.mozilla.org
  2. Smashing Magazine (2021). "Using Modern Image Formats: AVIF And WebP." Comprehensive comparison of lossy and lossless compression. smashingmagazine.com
  3. Ctrl blog (2024). "Comparing AVIF vs WebP file sizes." Data on compression efficiency across formats. ctrl.blog
  4. Cloudinary (2024). "Image Optimization: Lossy vs Lossless." Industry guide to compression techniques. cloudinary.com
  5. Mozilla ImageOptim (2024). "MozJPEG." Documentation for advanced JPEG optimization. github.com/mozilla/mozjpeg

Optimize Your Images with the Right Compression

Our tool automatically selects the best compression method for each image

Try Image Optimizer

Enjoying Image Optimizer?

This free tool costs money to run. Support development and hosting!