Skip to content
Crop DeskFree tools for photos, PDFs & everyday work Search tools…/Search tools…/Search tools…/Get apps

Home › Guides

PNG and DEFLATE: How Lossless Image Compression Works

By Mahendra Singh Deora · Published 23 November 2026 · 5 min read

PNG is the lossless image format of the web. Its rules are published by the W3C as the Portable Network Graphics (PNG) Specification, and its compression is DEFLATE, defined in IETF RFC 1951 and wrapped in the zlib format of RFC 1950. For students, PNG is a clean example of how a real file format combines a container, prediction and a general-purpose compressor. For everyday users, it explains why a screenshot can be smaller as a PNG while a photo is much bigger.

In short: a PNG file is an 8-byte signature followed by chunks. The pixel rows are first "filtered", meaning each byte is predicted from its neighbours and only the difference is stored. The filtered bytes are then compressed with DEFLATE, which is LZ77 back-references plus Huffman coding. Nothing is rounded away, so the decoded pixels are identical to the original.

The file layout: signature and chunks

The specification says the first eight bytes of every PNG are always 89 50 4E 47 0D 0A 1A 0A (hexadecimal). Bytes 2–4 are the ASCII letters "PNG". The carriage-return, line-feed and other control bytes help detect files damaged by text-mode transfers. After the signature comes a series of chunks, starting with IHDR and ending with IEND. Each chunk has:

FieldSizeMeaning
Length4 bytesNumber of bytes in the data field only
Chunk type4 bytesFor example IHDR, PLTE, IDAT, IEND
Chunk dataLength bytesMay be empty
CRC4 bytesError check over type and data

IHDR holds width (4 bytes), height (4 bytes), bit depth, colour type, compression method, filter method and interlace method (1 byte each), so its data is 13 bytes. The colour type values are 0 greyscale, 2 truecolour, 3 indexed-colour, 4 greyscale with alpha and 6 truecolour with alpha. The compressed pixels live in one or more IDAT chunks.

Step 1: filtering (prediction)

Before compression, each scanline (row) gets a filter-type byte, and every byte in the row is replaced by its difference from a prediction. The specification defines five filter types for filter method 0. Here x is the current byte, a is the byte to the left, b is the byte above, and c is the byte above-left:

TypeNameStored value
0Nonex
1Subx − a
2Upx − b
3Averagex − floor((a + b) / 2)
4Paethx − PaethPredictor(a, b, c)

Worked example (example values). A smooth grey gradient row has bytes 10, 12, 14, 16, 18. With the Sub filter it becomes 10, 2, 2, 2, 2. The original has five different values. The filtered row is almost all the same value, which a compressor handles very well.

The Paeth predictor computes p = a + b − c and picks whichever of a, b or c is closest to p, testing them in that order. The specification says the comparison order "is critical and shall not be altered". Example: a = 100, b = 110, c = 105 gives p = 105, and c is the closest (distance 0), so the prediction is 105.

Step 2: DEFLATE (LZ77 + Huffman)

RFC 1951 describes DEFLATE as a combination of two ideas:

  • LZ77 back-references. If a sequence of bytes has already appeared, the encoder writes a pair <length, backward distance> instead of repeating it. In DEFLATE, lengths run from 3 to 258 bytes and distances can go back up to 32K bytes. The PNG specification likewise says compression method 0 uses a sliding window of at most 32768 bytes.
  • Huffman coding. Literal bytes and length/distance codes are written with variable-length bit codes, so frequent symbols use fewer bits.

The filtered row 10, 2, 2, 2, 2 is ideal: after the first two bytes, the rest is one back-reference ("repeat the previous byte three more times").

Why PNG suits some images and not others

ImagePNG behaviourBetter choice for forms
Screenshot, text, line art, signature on whiteLarge flat areas and repeats, so it compresses very well and edges stay sharpPNG if accepted; otherwise a high-quality JPG
Camera photoSensor noise makes every byte slightly different, so there are few repeats and the file is largeJPG – see how JPEG compression works
Logo with transparencyAlpha channel supported (colour types 4 and 6)PNG

This is why exam portals that ask for a JPG under 50 KB rarely accept a PNG photo. A photo saved as PNG is lossless, but it can easily be ten times bigger. JPG vs PNG for online forms covers the practical rules.

Lossless does not mean "smallest possible"

Different PNG encoders can produce different file sizes for exactly the same pixels, because the specification leaves choices to the encoder: which filter to use on each row, and how hard DEFLATE searches for matches. All of them decode to identical pixels. That is why "PNG optimisers" can shrink files without any quality change. It is also why converting a JPG to PNG never restores quality already lost: the PNG just stores the damaged pixels losslessly.

Try it yourself

  1. Take a screenshot of a page of text and save it as PNG and as JPG at quality 0.8 using the image compressor or image converter. Compare KB and zoom in on the letters.
  2. Do the same with a camera photo. The size ranking usually flips.
  3. Open a PNG in a hex viewer and find the signature and the letters I H D R at bytes 12–15. Bytes 8–11 are the IHDR length, 00 00 00 0D (13).

Compare PNG and JPG sizes for your own image.

Open the image compressor

Frequently asked questions

Is PNG always lossless?

Yes. PNG decoding returns exactly the stored pixels. A PNG can still look bad if it was made from an already-compressed JPG, because it stores those pixels exactly.

Why is my photo bigger as PNG than as JPG?

Camera noise makes neighbouring pixels slightly different, so filtering and DEFLATE find few repeats. JPEG discards detail you barely see, so it is far smaller for photos.

What does the Paeth filter do?

It predicts each byte from the left, upper and upper-left bytes using p = a + b − c, picks the closest of the three, and stores only the difference.

Sources

Sources checked on 23 November 2026. Official notices and orders change; the latest version on the issuing body's website is final.