Image Compression Explained: Lossy vs Lossless
Learn the differences between two methods of reducing image file size and how to choose the right one.
Why Is Image Compression Necessary?
With advances in high-resolution cameras and smartphones, image file sizes continue to grow. A single photo exceeding 10MB is common. Such images slow down website loading and take up significant storage space.
Full-size images are unnecessary for most situations like email attachments, social media uploads, and website posts. Properly compressed images can significantly reduce file size while maintaining visual quality.
Image optimization is also essential for web performance. Google uses page loading speed as a ranking factor, and it directly impacts user experience.
What Is Lossy Compression?
Lossy compression reduces file size by removing information that's difficult for the human eye to distinguish. JPEG is a representative lossy compression format.
Higher compression ratios result in smaller file sizes but gradually degrade image quality. Choosing an appropriate quality setting (usually 70-85%) can reduce file size by 60-80% without noticeable quality loss.
Lossy compression is effective for photos, complex gradients, and natural images. However, compression artifacts may be noticeable in images with text, logos, or sharp edges.
What Is Lossless Compression?
Lossless compression reduces file size while preserving all image information. PNG is a representative lossless format. Decompressing yields an image identical to the original.
Lossless compression works by efficiently storing repeating patterns. It shows high compression rates for simple graphics, logos, icons, and screenshots.
Lossless compression has limited effectiveness for complex images like photos. In such cases, lossy compression is a more efficient choice.
WebP: Advantages of the Modern Image Format
WebP is a modern image format developed by Google that supports both lossy and lossless compression. It provides 25-35% smaller file sizes than JPEG and 26% smaller than PNG at the same quality.
Most modern browsers (Chrome, Firefox, Edge, Safari) currently support WebP. Converting website images to WebP can significantly improve page loading speed.
TheWebGyver's image compression tool supports conversion between JPEG, PNG, and WebP. Choose the optimal format for your situation to optimize your images.
The Real Difference Between Lossy and Lossless Is Whether You Can Undo It
It is easy to tell the two apart by file size alone, but the real dividing line is whether the original can be restored exactly after compression. The PNG specification defines lossless as a method of data compression that permits reconstruction of the original data exactly, bit for bit, and lists losslessness among its design goals, saying filtering and compression should preserve all information. JPEG, by contrast, is a lossy format based on the discrete cosine transform (MDN), and web.dev describes lossy compression as reducing image accuracy through quantization. The space saved by lossy compression is paid for with information that was thrown away.
Looking inside PNG makes the distinction clearer. PNG compression method 0 is deflate with a sliding window of at most 32,768 bytes, and the filtering step before it is, according to the specification, deterministic, reversible and lossless. That is why a PNG compression-level option changes only file size and processing time, never pixel values. In the measurements below, files saved with Pillow's PNG optimize option were read back and compared with the source pixels, and they matched exactly for both inputs.
A lossy quality number, on the other hand, is a dial for how much to discard, and the same number does not mean the same visual quality across formats. web.dev also says there is no universal setting suitable for all cases. In the measurements below, JPEG at quality 75 and WebP at quality 80 had similar PSNR against the source, 36.0 dB and 35.7 dB, yet the files were 339,085 and 253,728 bytes. That is why you should not compare formats by their quality numbers alone.
Measured Differences Between a Photo and a Screenshot
The figures below are from measurements run locally while preparing this article (an Apple Silicon Mac, Python Pillow 12.1.1 and cwebp 1.6.0). There are two inputs. One is NASA's Apollo 17 photo of the Earth, released into the public domain (Wikimedia Commons), resized to 1600 pixels wide (1600 by 1601). The other is a 1600 by 1000 region of a screenshot of a code editor on a dark background. The quality metric is PSNR against the source pixels; higher means closer to the source.
For the photo, lossless PNG was 4,305,953 bytes. JPEG was 886,649 bytes at quality 95 (20.6% of the PNG), 466,412 bytes at 85 (10.8%), 339,085 bytes at 75 (7.9%) and 226,951 bytes at 50 (5.3%), with PSNR falling to 39.2, 37.1, 36.0 and 34.4 dB. Lossless WebP was 3,470,728 bytes, 80.6% of the PNG. For this photo, whether lossy compression was used mattered far more than the difference between lossless formats.
For the screenshot, PNG was 178,623 bytes. JPEG at quality 85 was 178,465 bytes, almost the same as the PNG, and at quality 95 it was 276,190 bytes, 54.6% larger than the PNG. At quality 75 it dropped to 144,487 bytes (80.9% of the PNG), but PSNR fell to 36.9 dB. Lossless WebP was the smallest at 47,384 bytes (26.5% of the PNG), and lossy WebP at quality 80 was 81,080 bytes. For an image full of text and flat color, lossy JPEG offered little size advantage.
These results come from one photo and one screenshot, so they cannot be generalized to all images. The photo's source file was already a JPEG, which is another limitation. MDN says lossless WebP is typically 26% smaller than PNG, whereas for these inputs it was 19.4% smaller for the photo and 73.5% smaller for the screenshot, a wide spread. PSNR is a mathematical error metric and can differ from perceived quality, so zoom in and check important images yourself.
Does Repeated Lossy Compression Really Keep Making Things Worse?
It is commonly said that every re-save of a JPEG lowers quality further. In this measurement that was only half true. Saving the same photo 20 times in a row at quality 75 with Pillow left PSNR against the source almost unchanged: 36.03 dB after one save and 36.01 dB after twenty. Alternating between quality 90 and 70 for ten saves gave 35.52 dB, not far from the 35.66 dB of a single save at quality 70. This appears to happen because values that have already been quantized change little when saved again under the same conditions.
The result changed once the image was cropped by a few pixels before each save. Cropping 3 pixels off the left edge each time and saving at quality 75, PSNR fell from 36.03 dB after one round to 33.38 dB after five and 31.76 dB after ten. When a step that shifts the pixel grid, such as cropping or resizing, is part of the chain, losses can accumulate. That offers a clue to why images that are downloaded, edited and re-uploaded many times can look worse.
The practical conclusion is simple. Keep intermediate editing files in a lossless format such as PNG, and apply lossy compression once, at the end, for the version you distribute. Note that this observation comes from a single environment, Pillow, and other encoders or settings may behave differently.
How to Choose for Your Situation
Images whose colors change continuously, such as photographs, hide discarded detail well, so lossy compression suits them. MDN likewise says photographs and other continuous-tone images can usually tolerate lossy compression. Screenshots, diagrams, logos and line art are the opposite: blurring and colored fringes around text and sharp edges are very visible, so lossless is recommended. For simple shapes and icons, SVG, which stays sharp at any size, may be the better choice.
When you need a transparent background, JPEG is out, because it does not support an alpha channel. Google's WebP documentation says replacing a transparent PNG with lossy WebP plus alpha gives 60-70% size savings on average. It is still lossy compression, though, so check that transparent edges and shadows stay clean.
MDN says lossy AVIF images are around 50% smaller than JPEG, but it also notes that AVIF does not support progressive rendering, so the file must be fully downloaded before it can be displayed. Whichever format you pick, as web.dev advises, vary the compression level to find a good compromise between size and quality, and confirm the result meets your standards yourself. TheWebGyver's image compression tool has a before-and-after preview that makes this check easy.
References
Experience what you learned in this article with Image Compressor and Resizer.
Image Compressor and Resizer