PNGifier

How Does PNG Compression Work?

PNG compression is lossless, so the image you decode is identical to the original. It works in two stages: scanline filtering rewrites each row of pixels into mostly small numbers, then the DEFLATE algorithm — the same one used by zip — squeezes that data with LZ77 back-references and Huffman coding.

By Published

Is PNG lossless?

Yes. PNG uses lossless compression, which means the decoded image is identical to the original down to the last pixel. Unlike JPG, nothing is thrown away to save space, so you can open, edit, and re-save a PNG as many times as you like without any gradual loss of quality. If you want the background story on the format itself, see what is a PNG.

Step 1: how does scanline filtering work?

Before any compression happens, PNG transforms each row — or scanline — of pixels using one of five filter types: None, Sub, Up, Average, and Paeth. Each filter predicts a pixel from its neighbours (to the left, above, or both) and stores only the difference. In smooth or repetitive areas those differences are mostly tiny numbers close to zero, which are far easier to compress than the raw colour values. The encoder can pick a different filter for every row to get the best result.

Step 2: what is DEFLATE compression?

The filtered data is then compressed with DEFLATE, the same algorithm used by zip files. DEFLATE combines two techniques: LZ77, which replaces repeated sequences of bytes with short back-references to where they appeared earlier, and Huffman coding, which assigns shorter codes to the values that occur most often. Because filtering has already turned the pixels into lots of small, repeating numbers, DEFLATE has plenty of patterns to exploit — and the result is still perfectly reversible.

Why does PNG beat JPG for graphics but lose for photos?

PNG shines on graphics with flat colour and sharp edges — logos, icons, screenshots, and line art — because those images contain long runs of identical pixels that the filters and DEFLATE compress very effectively. Photographs are the opposite: their constant tonal variation gives the filters little repetition to find, so a lossless PNG ends up much larger than a lossy JPG of the same photo. For a side-by-side breakdown of where each format wins, see PNG vs JPG.

How do optimisers shrink PNGs further?

A PNG optimiser squeezes out extra bytes without changing how the image looks. It searches harder for the best filter on each row and can reduce the colour palette — fewer distinct colours mean more repetition for DEFLATE to compress. The output stays lossless, so it still matches the original pixel for pixel. You can try this on your own files with the PNG compressor.

Frequently asked questions

Does PNG compression lose quality?
No. PNG is lossless, so the decoded image is identical to the original — every pixel is preserved exactly, no matter how many times you save it.
What are the five PNG filter types?
None, Sub, Up, Average, and Paeth. Each row of pixels can use a different filter, and the one that produces the smallest, most repetitive data is chosen.
Why is my PNG photo so large?
Photographs have constant tonal variation, so the filters rarely find repeating patterns for DEFLATE to compress. For photos, a lossy format like JPG is far smaller.
How does a PNG optimiser make files smaller?
It tries better filter choices per row and can reduce the colour palette, giving DEFLATE more repetition to work with — all without changing how the image looks.