Ch. 4 · Image Codecs

한국어日本語
5 min

PNG vs JPEG: How Lossless Image Compression Works

PNG gives back every bit of the original, but at a lower compression ratio. Learn how PNG filtering and DEFLATE work, and when to choose PNG over JPEG.

The opposite pole from JPEG is PNG (Portable Network Graphics). It is lossless compression: the original can be reconstructed without losing a single bit.

PNG's two weapons

① Filtering (prediction) — each pixel is predicted from its left, upper, and upper-left neighbors, and only the difference is stored (the principle is the same as the prediction from Chapter 3, except it does exact subtraction with no loss). ② DEFLATE compression — a combination of LZ77 (replacing repeated patterns with references to earlier positions) and Huffman coding. It is the same algorithm as zip files.

JPEGPNG
Lossy or losslessLossy (adjustable)Lossless
Best-suited contentPhotos, natural imagesScreenshots, logos, text, graphics
Alpha channel (transparency)Not supportedSupported
Compression ratioVery high (10–20×)Relatively low (2–4×)
Practical pitfall — choosing the wrong format

Saving a photo as PNG makes the file 5–10 times larger than the JPEG, and conversely, saving a text-heavy screenshot as JPEG leaves messy ringing artifacts around the letters. The principle from Chapter 1 that '4:2:0 is a disadvantage for content with sharp color edges' applies here as well — lossy compression is fundamentally optimized for 'natural gradients' and is weak on artificial sharp edges.

PNGPNG

A lossless image format that uses filtering prediction + DEFLATE (LZ77 + Huffman).

DEFLATEDEFLATE

A general-purpose lossless compression algorithm combining LZ77 and Huffman coding.

LosslessLossless

A method that reproduces data identical to the original after compression and decompression.

Check yourself

Why is it a bad idea to save a text-heavy screenshot as JPEG?