Transparent PNG alpha — what the fourth channel really stores

A transparent PNG is not a picture with holes in it. Every pixel still carries three colour bytes; a fourth says how much of what sits behind it shows through. Most edge trouble blamed on “bad transparency” is that byte meeting a background it never matched.

The fourth byte

A transparent PNG uses colour type 6: an R, G, B triple plus one alpha sample per pixel, at 8 or 16 bits. Per the PNG specification, 0 is full transparency and 2bitdepth−1 full opacity, intermediate values compositing against the background. The spec is explicit about colour too: “PNG does not use premultiplied alpha.”

That matters: a pixel at 30% opacity stores its full colour, not 30% of it. Straight alpha, also called unassociated. One decision in a 1996 format is behind most of this page.

Where the alpha in this file comes from

The keyer converts each pixel to YCbCr, measures its distance from the key colour in the Cb/Cr plane (luminance for a neutral backdrop) and turns that distance into alpha: 0 at or below the lower threshold, 1 at or above the upper one, a straight line between.

The lower threshold is (tolerance / 100) × 118 for a colour key, × 150 for a neutral one: default tolerance 25 opens a band 29.5 units wide on green, 37.5 on a white wall. A positive edge widens the ramp by (edge / 100) × 46 units for colour, 60 for neutral; a negative edge pushes the threshold inward instead.

At the default edge of 0 the two thresholds coincide and the matte is hard: alpha is only ever 0 or 255, and a default export has no partially transparent pixels at all. Fractional alpha exists only where you asked for a soft edge — at edge +100 with tolerance 25 on green the ramp runs 29.5 to 75.5 units, and that span is the whole soft edge.

Alpha is a threshold result, not a fade. In the shadow sweep, a backdrop at 65% brightness sits 29.3412 units away and lands on alpha 0 at tolerance 25; the same backdrop at 60% sits 33.5337 away and lands on 255, fully opaque and not keyed at all. Raise tolerance to 50 and both go to 0.

Why one file looks white here and dark there

Compositing is one line of arithmetic: out = C × α + B × (1 − α), where C is the colour stored in the pixel and B is whatever is behind it. At α = 0.1, ninety per cent of what you see is the background. Nothing in the PNG records what that background should be — the file is a promise, not a finished picture.

So one export can look fine on a checkerboard, pale on white and grimy on near-black, with no change to the file. The preview on the tool has Checker, Dark and Light buttons for exactly this: flip the frame between backings and watch the outline. A fringe on both is colour in the pixel; a fringe on one is the backing showing through coverage you did not mean to create.

That is arithmetic, not a measurement: everything here is measured on synthetic 320×240 fixtures against a binary ground truth.

Why a clean matte can still show a white rim

Four things produce the white or dark rim people report; only one is in the file.

What actually gets written

Export makes a canvas at the source’s full pixel size, clears it, draws the frame, keys it in place and encodes once as PNG. Nothing is composited onto white and nothing is stamped on. Each pixel keeps the source RGB — spill-adjusted if you asked — with alpha as round(α × 255), one of 256 levels.

The same pixels go into a PNG sequence’s frames; the transparent WebM path is a different route with different losses, covered in PNG sequence vs transparent WebM and transparent WebM explained.

Checking a file before you hand it over

Put the result on Dark, then Light, and watch the outline rather than the middle of the subject. Visible on both means colour: move spill, or trim with a negative edge. Visible on one means coverage: lower the feather. If a destination shows it identically on black and white, its alpha was flattened on the way in.

Keep the PNG as master. Once a copy has been through a JPEG or a web export, the fourth channel is gone.

When this does not work

Where the numbers come from

Thresholds and the feather and trim scales come from the keyer in the reference repository; the alpha column above is from its shadow sweep on a synthetic 320×240 fixture, not real footage. The write-up is archived at doi.org/10.5281/zenodo.22916767; the format rules are the PNG specification. For a photo, the photo walkthrough covers the same export.

Open the green screen remover