What is lossless compression? How PNG keeps every pixel

Published , by the PrintBlob team.

Lossless compression is a way of storing data in fewer bytes that allows the original to be reconstructed exactly, bit for bit. For an image, it means the pixels that come out are identical to the pixels that went in.

It is why print files are usually PNG: a transparent PNG at the printed size can be saved, opened and sent as many times as needed without a pixel changing.

How PNG does it

The W3C’s PNG specification defines lossless compression as a method that permits reconstruction of the original data exactly, bit for bit. PNG achieves it in two steps:

  1. Filtering. Each row of pixels is transformed by one of five reversible filters (None, Sub, Up, Average or Paeth), chosen so that the data becomes more compressible. A filter loses nothing; it only rearranges.
  2. Deflate. The filtered rows are compressed with deflate, an LZ77-derived method with a sliding window of at most 32,768 bytes, stored in zlib format.

The specification says the reference image can always be recovered exactly from a PNG file. Information can only be lost before the image reaches PNG, such as when a JPEG is converted to PNG: the PNG then holds a faithful copy of an already damaged image.

What PNG stores

Greyscale at 1, 2, 4, 8 or 16 bits; true colour at 8 or 16 bits per channel; indexed colour with a palette; and greyscale or true colour with an alpha channel. The alpha channel is how a transparent background is kept: zero means fully transparent, the maximum fully opaque. If every pixel is opaque the alpha channel can be left out. This is what makes a PNG right for DTG and DTF, which print every pixel that is not transparent.

What lossless does not do

  • It does not make a small image bigger or sharper. Lossless keeps what is there; see upscaling.
  • It does not guarantee a small file. A detailed photograph in PNG is far larger than the same photograph as a JPEG. Cloudinary’s advice is to edit and store originals in lossless formats such as PNG or TIFF and save as JPEG only at the end, when size matters more than exactness.
  • It does not undo earlier damage. Converting a JPEG to PNG stops further loss but does not remove the artefacts already there.
  • It does not remove the edge smoothing. The soft pixels along the edge of text and shapes are part of the image; see anti-aliasing.

When to use lossless formats

Situation Use
Print file for DTG or DTF Transparent PNG at the printed size
Logo with flat colours or text PNG
Working file you will edit again PNG or TIFF
Photo for a web page where size matters JPEG or WebP, saved once

In PrintBlob

Each printed side is rendered as a transparent PNG at exactly the print area’s size at 300 DPI (the front’s in the free plugin, every other side’s with PrintBlob Pro), so a 28 × 36 cm print is 3307 × 4252 pixels. PNG is lossless, so that file is a faithful copy of what the browser rendered. Customers’ uploads are accepted as PNG, JPEG or WebP and kept as they were uploaded. A JPEG stays a JPEG with its losses; PrintBlob does not change it. See print files.

Sources