The word “compression” makes many people nervous — they picture blurry photos and blocky artifacts. But not all compression degrades image quality. With the right techniques, you can dramatically reduce file sizes while keeping images looking virtually identical to the original. This guide explains the difference between lossy and lossless compression and walks through every major technique for preserving quality while shrinking file sizes.
Understanding this fundamental difference is the starting point for choosing the right approach:
Lossy compression permanently discards some image data. The algorithm analyzes the image, identifies information that human eyes are less sensitive to (fine high-frequency details, subtle color gradients), and either removes or approximates that information. JPEG is the classic lossy format. The more aggressively you compress, the more noticeable the artifacts — typically manifesting as blocky “mosquito noise” around edges and loss of fine texture.
Lossless compression doesn’t discard any pixel data. Instead, it finds smarter ways to encode the same information. Think of it like replacing a sequence of “AAAAAAA” with “7×A” — the information is identical, just stored more efficiently. Decompress the file and you get back a bit-for-bit identical copy of the original.
For use cases requiring absolute fidelity — print production, product showcase photography, medical imaging — lossless is the only acceptable approach. For most web images, a well-tuned lossy compression is entirely sufficient.
PNG uses lossless compression (DEFLATE algorithm) by default. However, many PNG files aren’t optimally compressed and can be significantly reduced further.
Technically, pngquant applies lossy compression by reducing PNG color depth from millions of colors to 256 or fewer. But the algorithm is so precise that the visual difference is nearly imperceptible, while file sizes drop by 40–70%.
pngquant --quality=65-80 input.pngThese tools recompress the DEFLATE data stream to find a more efficient encoding without changing any pixel values:
optipng -o7 input.png (-o7 is maximum compression level)oxipng -o max input.png (built in Rust, significantly faster)JPEG is inherently a lossy format, but there are ways to shrink JPEG files without re-encoding — which means no additional quality loss.
Camera-captured JPEGs contain embedded metadata: GPS coordinates, camera model, aperture and shutter settings, ICC color profiles, and more. None of this information affects how the image looks on screen, but it can account for 10–30% of the total file size.
jpegtran -copy none -optimize input.jpg > output.jpgexiftool -all= input.jpg (strips all metadata)jpegtran can also re-optimize the Huffman coding tables in a JPEG (using the -optimize flag) for a smaller file, and convert to progressive JPEG (-progressive), which loads as a low-resolution image that gradually sharpens — improving perceived performance without affecting final image quality.
WebP is a versatile format that supports both lossy and lossless compression, with better efficiency than JPEG and PNG respectively:
cwebp -lossless input.png -o output.webpcwebp -q 80 input.jpg -o output.webpAll modern browsers — Chrome, Firefox, Safari 14+, Edge — support WebP. Use the HTML <picture> element to serve WebP with a JPEG fallback for older browsers.
AVIF (AV1 Image File Format) is currently the most compression-efficient image format available, derived from the open-source AV1 video codec:
Here are the best tools for different situations:
Beyond eyeballing the result, objective metrics can help you verify compression quality:
<picture> element to serve AVIF or WebP with appropriate fallbacks“Lossless” image optimization isn’t a single technique — it’s a combination of approaches: stripping unnecessary metadata, re-encoding with more efficient algorithms, and leveraging modern formats designed for better compression. For most web images, combining modern format support (WebP or AVIF) with well-tuned compression settings can achieve results that look identical to the original while loading in a fraction of the time.
Do it in your browser — the file never leaves your device: Image Optimize