Compression
How PNG compression works, and why some PNGs are 18x bigger
PNG never throws a pixel away, so file size is decided before compression starts: by colour type, bit depth and the filters an encoder picks. We measured what each decision costs.
At a glance
- PNG is lossless: filters and DEFLATE rearrange bytes, they never discard pixels, and there is no quality setting anywhere in the format.
- Encoding runs in two stages per scanline — one of five filter types, then DEFLATE inside a zlib stream with a window of at most 32,768 bytes.
- Colour type and bit depth fix the raw data before compression begins: 4 bytes per pixel for truecolour with alpha, 3 for truecolour, 1 for an 8-bit palette image, and one bit per pixel for bilevel greyscale.
- In our conversion benchmark the same 1600x1200 photo measured 1106.8 KB as PNG against 60.7 KB as the WebP it was decoded from: 18.2x.
- A smooth gradient is PNG's worst case — 9.3 KB as WebP, 136.9 KB as PNG (14.7x) — because a lossy codec can write "this trends downward" and a lossless one cannot.
- Flat shapes and few colours are PNG's best case: a 1200x1200 flat illustration came out at 190.2 KB, and an 800x800 logo including its alpha channel at 70.3 KB.
File size is decided before compression starts
PNG makes one promise no lossy format can make: what you decode is what was encoded. The specification lists "losslessness: filtering and compression should preserve all information" among its design goals, and the format carries no quality parameter at all. There is no "close enough" in a PNG, which is why the same picture can be 60 KB in one converted file and 1,106 KB in another.
Three inputs set that number, and only one of them is still adjustable after you pick up a file.
- Raw sample data. Colour type and bit depth decide how many bytes each pixel occupies before anything is compressed. This is the biggest lever and the one most guides skip.
- Encoder effort. Filter choice per scanline and the DEFLATE level change the result by a few percent each, and palette-aware encoders change it by a lot on the right image.
- The content. Noise, gradients and soft edges carry information DEFLATE cannot predict. No setting rescues them.
A PNG file is small on disk and large in structure: a signature, an IHDR chunk holding dimensions and the seven bytes that describe the image, optional chunks such as PLTE and tRNS, one or more IDAT chunks with the compressed data, and a closing IEND. A four-byte CRC guards every chunk, so corruption is detected rather than rendered. Only one compression method exists in the standard — method 0, DEFLATE — and only one filter method.
The pipeline: filter each scanline, then DEFLATE the result
Filtering is a transformation applied to scanlines "with the aim of improving their compressibility". It is not compression and it loses nothing: filtered output is the same number of bytes as the input, plus one byte per scanline recording which filter was used. Those filter-type bytes are included in the datastream that goes to the compressor, so even the "None" filter costs a byte per row.
The idea is prediction. Instead of storing pixel values, the encoder stores the difference between each byte and a prediction of that byte, and DEFLATE then compresses a stream that is mostly zeros and small numbers. For a flat UI panel, every byte in a row predicts the next one exactly; for a vertical gradient, each row is nearly identical to the one above it.
| Filter | What it predicts each byte from | Usually wins on |
|---|---|---|
| 0 — None | Nothing; the byte is stored unchanged | Noisy or already-random data, palette indexes |
| 1 — Sub | The byte immediately to the left | Horizontal shading, smooth left-to-right ramps |
| 2 — Up | The byte directly above | Vertical gradients, repeated rows in UI chrome |
| 3 — Average | Half the sum of left and above | Soft photographic texture with no dominant direction |
| 4 — Paeth | Whichever of left, above or upper-left is closest to a local linear fit | Mixed-direction detail; the general-purpose choice |
Filters work on bytes, not pixels, regardless of colour type — which is why a bit depth below 8 gets no special treatment, and why the alpha plane is filtered exactly like colour data. Reconstruction needs the previously decoded byte to the left, the byte above, and the byte above-left, in that order.
Everything after filtering is ordinary DEFLATE: LZ77 matching plus Huffman coding, wrapped in a zlib stream with a check value, "deflate compression with a sliding window ... of at most 32768 bytes". No preset dictionary is allowed. If the data is 16,384 bytes or smaller, an encoder may shrink the window to save memory on both ends without hurting the ratio.
Two details matter when you are troubleshooting a size problem. First, multiple IDAT chunks are legal and must appear consecutively, but they concatenate into a single zlib datastream — splitting them does not compress each piece separately. Compressed text chunks such as zTXt and iTXt are the exception: each holds its own independent zlib stream. Second, the encoder is allowed to switch filters per scanline: "an intelligent encoder can switch filters from one scanline to the next. The method for choosing which filter to employ is left to the encoder." That sentence is the reason two tools produce different sizes for identical pixels.
Encoder effort has limits, though. In Pillow, the PNG writer's ZLIB level runs 0–9 and defaults to 6; passing optimize=True ignores any level you set and uses 9 with extra search work, which is the setting behind the PNG sizes quoted on this site. On a photographic image, going from level 6 to level 9 typically buys a few percent. Reshaping the raw data buys an order of magnitude.
Colour type and bit depth: the matrix that sets your ceiling
PNG defines five image types, each with a numeric colour type, and each colour type permits only certain bit depths:
| PNG image type | Colour type | Allowed bit depths | Bytes per pixel at 8-bit |
|---|---|---|---|
| Greyscale | 0 | 1, 2, 4, 8, 16 | 1 |
| Truecolour (RGB) | 2 | 8, 16 | 3 |
| Indexed-colour (palette) | 3 | 1, 2, 4, 8 | 1 |
| Greyscale with alpha | 4 | 8, 16 | 2 |
| Truecolour with alpha (RGBA) | 6 | 8, 16 | 4 |
The specification keeps this list short on purpose: "bit depth restrictions for each colour type are imposed to simplify implementations and to prohibit combinations that do not compress well."
The consequences are easy to understate. A 4000x3000 screenshot as RGBA is 12,000,000 pixels at four bytes each — 48 million bytes of samples to squeeze before a single line of DEFLATE runs. Store the same screen as an indexed-colour PNG at bit depth 8 and the raw data is 12 million bytes; at bit depth 4 with a 16-entry palette it is 6 million. The image still looks identical, because a UI screenshot usually contains fewer than two dozen distinct colours.
Palettes have a hard ceiling worth internalising: PLTE holds "from 1 to 256 palette entries, each a three-byte series" of red, green and blue, and the number of entries cannot exceed what the bit depth can address — at most 16 entries at bit depth 4, at most 256 at bit depth 8. There is no alpha in the palette itself; indexed transparency arrives through a tRNS chunk, which supplies a one-byte alpha value per palette entry and may be shorter than the palette, in which case the remaining entries are fully opaque.
Two more rules bite people in practice. Indexed images always carry a sample depth of 8 no matter what the bit depth says, because palette entries are stored at 8 bits per channel — quantising a PNG to a palette can therefore never exceed 256 colours, however carefully you choose them. And 16-bit samples are not a bigger version of the same picture for most purposes: they double the raw data and usually encode precision the source never had, which is a common reason a "photo I exported from Lightroom" arrives at 3 MB.## Interlacing: progressive display, paid for in bytes
Interlace method 1, known as Adam7, splits the image into seven passes and transmits them in order, so a decoder can show a coarse version of the whole picture after the first pass instead of wait for every scanline. Each pass carries a subset of pixels defined by an 8-by-8 pattern, and within a pass the pixels are transmitted left to right, top to bottom. Images narrower or shorter than five pixels leave some passes empty.
The trade-off is stated plainly in the specification: "on average, interlacing slightly expands the datastream size, but it can give the user a meaningful display much more rapidly." Every pass gets its own filter bytes and its own copy of the per-scanline state, and the vertical neighbours that make Up and Paeth effective sit 8 rows apart instead of 1, so the filtered data is less predictable. If an interlaced PNG seems larger than an equivalent file, that is why — and non-interlaced is the right default for anything served from a fast source.
What makes a PNG huge, and what makes it tiny
The pattern is consistent enough to plan around, and our conversion benchmark puts numbers on it. Those figures come from six synthetic images — a photograph, a UI screenshot, a transparent logo, flat vector-style art, a cutout product photo and a smooth gradient — converted from a WebP at quality 80 and re-encoded as PNG with optimize=True.
| Image | WebP q80 source | PNG output | PNG / source |
|---|---|---|---|
| Photo 1600x1200 | 60.7 KB | 1106.8 KB | 18.2x |
| Screenshot 1920x1080 | 157.7 KB | 1157.2 KB | 7.3x |
| Logo 800x800 (alpha) | 7.9 KB | 70.3 KB | 8.9x |
| Flat art 1200x1200 | 24.7 KB | 190.2 KB | 7.7x |
| Product cutout 1400x1400 (alpha) | 25.0 KB | 401.5 KB | 16.1x |
| Gradient 1600x900 | 9.3 KB | 136.9 KB | 14.7x |
The ordering is instructive because it is not the ordering most people expect. The logo, which has four channels per pixel, is the smallest PNG in the table; the gradient, which has three, is the second worst ratio. Alpha is expensive only when it contains soft information — the product cutout's 1,960,000 pixels include a large soft-edged mask, and the file came out 16.1x the WebP even though most of the canvas is fully transparent.
Four properties drive PNG size up:
- Noise. Camera grain, film texture and dithering are, by definition, unpredictable. DEFLATE's back-references find nothing to match; each value lands as fresh literals. In the same corpus, the unencoded photo PNG measured 3167.5 KB while flat art measured 16.1 KB at identical depth and colour type.
- Smooth gradients. A ramp changes value slightly on every pixel, so
SubandUpresiduals are small but never zero — nothing to back-reference, nothing to Huffman-compress. The gradient above is 9.3 KB in a lossy codec that can model the trend analytically and 136.9 KB in PNG. - Photographic colour. Three or four channels of continuous tone per pixel is simply a lot of information to record exactly, and photographs are the one case where a lossy format is the right tool.
- Soft alpha. A hard-edged mask is nearly free: the spec observes that "alpha is typically either zero or one for the vast majority of pixels". A feathered mask is not, because every semi-transparent pixel is a distinct value in a fourth channel.
And three properties drive it down:
- Flat colours. Large areas with one exact value filter to rows of zeroes, which DEFLATE represents in a few bytes. This is why PNG beats JPEG on most interface screenshots.
- Few distinct colours. Indexed-colour PNG turns a megapixel canvas into one byte per pixel with a shared palette, and 1-, 2- and 4-bit indexes shrink it further.
- Hard edges and text. High contrast is not a problem for PNG, only for block-based lossy codecs. Anti-aliased type costs a little (the edge pixels carry intermediate values) and nothing else.
Where APNG fits
Animated PNG is no longer an extension bolted onto the side of the format: it is part of the PNG specification in its current edition, defined by three additional chunk types. acTL must appear before any IDAT chunk to declare the animation; fcTL carries per-frame placement and rendering information; fdAT holds frame data with the same structure as IDAT. Compatibility is handled gracefully — "a non-animated PNG decoder will ignore the ancillary APNG-specific chunks and display the static image" — so an APNG saved with the default image as its first frame behaves as a normal PNG everywhere.
The specification positions APNG as "a replacement for simple animated images that have traditionally used the GIF format, while adding support for 24-bit images and 8-bit transparency, which GIF lacks." Browser support reflects that history: MDN lists Chrome 59, Edge 12, Firefox 3 (APNG originated at Mozilla), Opera 46 and Safari 8.
Two practical consequences. Every frame is a full PNG image and therefore lossless, so a 40-frame animation is roughly 40 images' worth of bytes; if the destination is a browser and you control the markup, an animated WebP or AVIF is far smaller. And APNG has no separate "quality" — the only sane optimisation is the same one you apply to a still PNG: reduce colours, crop the canvas, and let the tool recompress.
What to do about a PNG that is too big
Work down this list rather than jumping to a format change or, worse, re-exporting at a smaller pixel size you cannot undo. Each step keeps more of the original than the next one.
| Situation | First move | What it costs |
|---|---|---|
| Photo, gradient or soft alpha PNG going to the web | Switch to WebP or AVIF, keep the PNG as the master | Lossy: no longer bit-exact |
| Screenshot or UI asset that must stay a PNG | Quantise to an 8-bit palette with pngquant | Lossy: 256 colours, plus dithering you may not want on flat panels |
| Logo or icon where exact pixels matter | Recompress losslessly with oxipng or Zopfli | Encoding time; pixels unchanged |
| Any PNG with needless metadata | Strip ancillary chunks (oxipng --strip safe) |
Time only; "can save a few kilobytes" |
| Dimensions far larger than the display size | Resize before compressing | Detail lost, but the best bytes-per-quality trade available |
| 16-bit PNG with no printing or HDR requirement | Convert to 8-bit | Possible banding in very soft gradients |
| A photograph you will publish as a photo | Do not ship it as PNG | PNG is 18.2x the WebP in our test |
Three tools cover most of this. pngquant performs lossy palette quantisation: "the conversion reduces file sizes significantly (often as much as 70%) and preserves full alpha transparency", with a quality range of 0–100 and its own adaptive dithering. Its published example turns a 75,628-byte PNG into a 19,996-byte one. oxipng is "a multithreaded lossless PNG/APNG compression optimizer" with optimisation levels -o 0 through -o 6; note that its --alpha option "can improve compression of images with transparency, by altering the color values of fully transparent pixels", which the tool's own documentation flags as technically lossy — it is applying the specification's advice that fully transparent pixels should share one colour value. Zopfli recompresses the same filtered data with a slower DEFLATE search: "very good, but slow", and oxipng's documentation warns that Zopfli is not guaranteed to beat its default compressor on every file.
What does not help much is nudging the DEFLATE level on an already-optimised PNG. Both settings solve the same problem and neither touches the raw data. If you want a smaller PNG, change what has to be recorded — colours, precision, dimensions — before you change how hard the compressor tries.
If you have a WebP in hand and need a PNG for a form, marketplace or editor that refuses anything else, our converter does the decode-and-re-encode step in your browser: convert WebP to PNG here. The measured cost of that conversion, image type by image type, is on the 2026 benchmark page.
Convert WebP files to PNG or JPG — in your browser
Batch conversion, ZIP download, transparency kept. No upload.
Sources
- PNG (Portable Network Graphics) Specification, Third Edition — filter types, zlib parameters, colour-type matrix, Adam7 and APNG — https://www.w3.org/TR/png-3/
- WebP compression documentation (Google) — lossy, lossless and lossy-RGB-with-lossless-alpha modes — https://developers.google.com/speed/webp/docs/compression
- MDN: Image file format guide — APNG browser support and colour depth comparison — https://developer.mozilla.org/en-US/docs/Web/Media/Guides/Formats/Image_types
- pngquant — lossy PNG compressor, quantisation and dithering — https://pngquant.org/
- Oxipng — lossless PNG/APNG optimizer documentation — https://github.com/oxipng/oxipng
- Zopfli Compression Algorithm README (Google) — https://github.com/google/zopfli
- Pillow image file formats — PNG
compress_levelandoptimizebehaviour — https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html - Measured results used above: study-a-format.csv, study-b-jpeg-quality.csv — ./assets/bench/