Guide
Lossy vs lossless compression, explained
Lossless compression packs the same pixels into fewer bytes. Lossy compression changes the pixels, carefully, so they pack far better. Knowing which is which explains almost every surprise with image file sizes.
By Mark Stoumann ·
Two kinds of compression
An uncompressed photo is simply a grid of pixels, each with a red, green and blue value. A 12-megapixel phone photo at one byte per color is 12 million × 3 bytes, about 36 MB. Nobody stores photos that way, because two very different tricks make them far smaller:
- Lossless compression finds patterns and writes them down more efficiently, like writing “50 × blue” instead of listing blue fifty times. Decompressing gives back the original data exactly, bit for bit. PNG, GIF, lossless WebP and ZIP files work this way.
- Lossy compression first throws away information your eyes are unlikely to miss, then packs what is left. The result is close to the original, never identical, and often ten times smaller or more. JPG, lossy WebP, AVIF and HEIC work this way.
Neither is better in general. Lossless suits images that must stay exact, such as screenshots, diagrams and files you are still editing. Lossy suits photographs, where the fine noise that lossless compression struggles with is exactly what lossy compression can drop.
How JPG compression works
JPG (JPEG) dates from 1992 and is still the most widely used photo format. Its encoder works in four steps, and only two of them lose anything.
- Split brightness from color. The image is converted from red, green and blue into one brightness channel and two color channels. Our eyes notice fine changes in brightness much more than fine changes in color.
- Store color at lower resolution. Most encoders keep one color sample for every 2 × 2 block of pixels, known as 4:2:0 chroma subsampling. That alone halves the data, and on photos it is very hard to see. It shows as fuzzy edges on bright red text or thin colored lines, which is why screenshots suffer more than photos.
- Describe each 8 × 8 block as a mix of patterns. The image is cut into blocks of 8 × 8 pixels, and a calculation called the discrete cosine transform (DCT) rewrites each block as 64 numbers: one for its average tone, and the rest for patterns from smooth gradients to fine checkerboards. This step loses nothing by itself.
- Round off the fine detail. Each of the 64 numbers is divided by a value from a quantization table and rounded. The values for fine patterns are large, so much of that detail rounds to zero, and long runs of zeros pack into very few bytes. This rounding is where most of the loss happens.
The quality setting is a single knob for step 4: it scales the quantization table. Higher quality means smaller divisors, less rounding and a bigger file. The widely used libjpeg-turbo library suggests 50–95 for photos, with 75 as its default, and warns that above about 95 files grow dramatically for a gain people rarely see. At very low quality, the 8 × 8 grid becomes visible as blocks, and sharp edges get a halo of “mosquito noise”.
This also explains generation loss: saving a JPG again runs the rounding again on pixels that were already rounded, and each round adds small errors. Edit from the original, and export to JPG once at the end.
How PNG stays lossless
PNG compresses in two steps, both fully reversible.
- Filtering. Each row of pixels can be rewritten as differences from its neighbors: from the pixel to the left (Sub), the pixel above (Up), their average (Average), or a smart guess from three neighbors (Paeth). In a smooth sky, the differences are mostly tiny numbers that repeat, which compress much better than the raw colors.
- DEFLATE. The filtered data is packed with the same method as ZIP files, which replaces repeated byte sequences with short references to an earlier copy, up to 32 KB back.
That is why PNG shines on screenshots, logos and diagrams, which are full of flat color and repeated patterns, and struggles with photos, where camera noise means almost no two neighboring pixels are the same. A photo saved as PNG is often several times larger than a good JPG of it, while a screenshot of text can be smaller as PNG than as JPG, and sharper.
Because the pixels can’t change, the only way to make a PNG much smaller is to change what is stored: fewer pixels (resize it), fewer colors, or a different format. For a photo, the format is what counts: you can convert the PNG to JPG and keep the original. Saving a PNG again with a different program sometimes saves a little and sometimes adds a little, depending on how hard each encoder searches for the best filters.
WebP, AVIF and HEIC: video codecs for still images
The newer formats borrow from video compression, where squeezing every frame matters most. They still round off detail like JPG, but they first predict each block from its already-decoded neighbors and only store the difference, use blocks of several sizes instead of one fixed 8 × 8 grid, and can tune the rounding separately for busy and smooth parts of the picture.
| Format | Based on | Lossless option | Transparency |
|---|---|---|---|
| JPG | Its own DCT method (1992) | No (in practice) | No |
| PNG | Filters + DEFLATE | Always lossless | Yes |
| WebP | VP8 video codec (lossy); own method (lossless) | Yes | Yes |
| AVIF | AV1 video codec in a HEIF file | Yes | Yes |
| HEIC | HEVC (H.265) video codec in a HEIF file | Rarely used | Yes |
Google’s own measurements put lossy WebP at 25–34% smaller than JPG at the same measured quality, and lossless WebP at 26% smaller than PNG. WebP can also mix the two: lossy color with a lossless transparency layer, which is why a PNG cut-out converted to WebP keeps clean edges while shrinking a lot. Which of these formats you can safely use depends on where the image goes; see which image formats work where.
Choosing in practice
- Photos to share or put online: lossy. JPG for maximum compatibility, WebP for websites.
- Screenshots, logos, charts, line art: lossless PNG, or WebP at high quality for websites. Lossy compression smears sharp text edges first.
- Photos you will edit again: keep the original, or a lossless copy, and export a lossy version only at the end.
- A size limit to meet: lower the quality before shrinking the image, but not too far. TinyPixr’s compressor searches qualities between 95% and 40% for the highest that fits, and makes the image smaller in pixels rather than go below 40%, where JPG blocks start to show.
Frequently asked questions
Is 100% JPG quality lossless?
No. At quality 100 the quantization step barely rounds anything, but the colors are still converted and, with most encoders, the color detail is still stored at lower resolution. The libjpeg-turbo documentation says there is still information loss in subsampling and rounding. For a truly lossless copy, use PNG or lossless WebP.
Does a JPG lose quality every time I open it?
No. Opening, viewing, copying or renaming a JPG doesn’t change it. Quality is only lost when an app saves the image again as a new JPG. Rotating in some viewers does re-save, so if it matters, check whether your app rotates losslessly or just changes the orientation flag.
Is a 75% quality JPG the same in every app?
No. The JPEG standard has no quality scale; apps choose their own quantization tables and map their slider to them in their own way. 75 in one app can look different from 75 in another, so judge by eye and by file size rather than by the number.
Why does converting JPG to PNG make the file so much bigger?
PNG stores every pixel exactly, including the noise and the small blocky errors the JPG compression created. Those make the image hard to compress without loss. Nothing is gained in quality: the PNG looks exactly like the JPG did.
Which is better, lossy WebP or JPG?
For websites, usually WebP. Google measured lossy WebP files at 25–34% smaller than JPG at the same visual quality, and WebP can also store transparency. JPG remains the safer choice for email, documents and older software.