CHROMA KEY NOTES / 012
What Happens When the Input PNG Already Has Transparency
Nothing happens. The tool's scan loop checks the alpha channel before testing the color distance, and every pixel with alpha equal to zero is skipped entirely. On our 640 by 420 test image, feeding an already-keyed PNG back into the tool cleared zero pixels, found zero key candidates, and returned the input unchanged. If you need to re-key an already-transparent PNG, flatten it onto an opaque background color first, then feed the flattened result.
The scan loop's alpha gate
The tool processes pixels in three stages: edge-color detection, a connected scan that marks key candidates, and a matte pass that clears and feathers them. All three stages check the alpha channel before touching a pixel:
- Edge detection (app.js line 26):
if (data[i + 3] < 128) continue;skips transparent edge pixels when auto-detecting the dominant background color. - Scan (app.js line 164):
if (data[i+3] && distance(i) <= threshold) connected[p] = 1;uses JavaScript truthiness. Alpha zero is falsy, so the&&short-circuits anddistance()is never called. - Matte (app.js line 211):
if(!data[i+3])continue;skips transparent pixels during feathering and spill removal.
This means a pixel with alpha equal to zero is never tested as a key candidate, never marked as connected, and never refined. Its RGB values are left untouched in the ImageData array, and the output canvas preserves them as-is.
Six scenarios on the same sample image
We ran six variations through work/alpha-input-tests/probe.py, a pure-Python reimplementation of the tool's green-distance test and connected-component flood. The source image is the repo's own sample-green-screen.png (640 x 420, color type 2, no alpha, 209,102 key-colored pixels at the default tolerance of 24). Each scenario produces a different RGBA PNG that is then fed back through the full keying pipeline:
| Scenario | Background alpha | Candidates found | Pixels cleared | Edge color detected |
|---|---|---|---|---|
| A. Already keyed, RGB=(0,0,0) | 0 | 0 | 0 | null (fallback to preset) |
| B. Already keyed, RGB=green preserved | 0 | 0 | 0 | null (fallback to preset) |
| C. Partial alpha (128) | 128 | 209,102 | 209,102 | (0, 210, 106) detected |
| D. Semi-transparent subject over opaque green | 255 (bg), 128 (subject) | 209,102 | 209,102 | (0, 210, 106) detected |
| E. Re-feed the tool's own output | 0 | 0 | 0 | null (fallback to preset) |
| F. Mixed: half transparent, half still green | 0 and 255 | 104,553 | 104,550 | (0, 210, 106) detected |
Scenarios A, B, and E all produce zero cleared pixels because every background pixel has alpha equal to zero. Feeding its own output back as input changes no pixel and clears nothing: the 209,102 transparent pixels stay transparent, so the second pass on a 75 percent transparent image still decodes into a 209,102 pixel matrix rather than collapsing into an empty frame. Scenario B is particularly telling: the hidden RGB under the transparent pixels is still green (190 distinct values with a mean of (0.1, 210.0, 106.0)), but the tool never looks at it because the alpha gate fires before the color test. Scenario F shows a mixed state where half the background was already keyed and half was not; the tool only processes the half that still has opaque green, clearing 104,550 of the 104,553 remaining candidates.
Scenarios C and D confirm that partial alpha (any value from 1 to 255) is treated as fully opaque for keying purposes. Alpha 128 is truthy in JavaScript, so the scan proceeds normally and finds all 209,102 candidates. The boundary between "skipped" and "processed" is the integer zero, not 128 or any other threshold.
Hidden RGB: what surfaces when you drop the alpha channel
When a keyer sets alpha to zero, the original RGB values under those pixels may or may not be cleared. This matters when the already-transparent PNG is later converted to a format that has no alpha channel, such as JPEG. One Stack Overflow developer described the browser canvas behavior precisely: "Whenever I set a pixel value to have an alpha of 0, the corresponding RGB channels are also zeroed upon conversion to a PNG. Is this an implementation detail?" ([SO 11601302](https://stackoverflow.com/questions/11601302/html5-canvas-to-png-zeroes-all-channels-when-alpha-transparent)). Another developer found that a Gaussian blur on an RGBA PNG produced dark output: "if without alpha channel, it works correct, but when i have alpha channel with png, the result will become too dark" ([SO 35476142](https://stackoverflow.com/questions/35476142/gaussian-blur-handle-with-alpha-transparency)).
Our measurements show two distinct behaviors depending on which tool wrote the PNG:
- Browser canvas export (
canvas.toBlob('image/png')): zeroes all RGB channels when alpha is zero. All 209,102 transparent pixels store RGB = (0, 0, 0). Dropping alpha later reveals a black box. - Custom RGBA writer (our pure-Python
write_png_rgba, color type 6): preserves the original green values. All 209,102 transparent pixels store 190 distinct green shades with a mean of (0.1, 210.0, 106.0). Dropping alpha later reveals a green box.
A .NET developer hit the same problem from the other direction: "it works fine except for when the png files contain transparency due to the alpha channel. In which case the converted jpeg has a black background. Is there any way to make the background white instead?" ([SO 6513633](https://stackoverflow.com/questions/6513633/convert-transparent-png-to-jpg-with-non-black-background-color)). Another developer's PHP script produced the same result: "when a PNG with transparency is uploaded, the resulting JPEG image is black where there was transparency" ([SO 5688954](https://stackoverflow.com/questions/5688954/how-to-replace-black-background-with-white-when-resizing-converting-png-images-w)). The color that surfaces is not chosen by the user; it is whatever RGB the source file hid under the alpha channel.
Why re-keying silently fails
The failure chain is four steps long and produces no error message:
- The user feeds an already-transparent PNG. All 209,102 background pixels have alpha equal to zero.
- The scan loop skips every transparent pixel because
data[i+3]is falsy. Zero candidates are found. - Edge-color detection returns null because all edge pixels have alpha below 128. The tool falls back to the green preset.
- The output is identical to the input. Zero pixels are cleared, zero error is shown, and the status bar reports "No matching screen color was connected to the image edge."
The tool does not crash, does not warn, and does not modify the image. A user who does not understand the alpha gate might conclude the tool is broken, when in fact it is correctly refusing to touch pixels it has been told are already transparent. One Photoshop user described a related confusion: "After reinstalling Windows I got a problem that transparency is lost when I paste image from clipboard, from another program(like browser). Instead of transparency photoshop using full black color" ([Super User 1329949](https://superuser.com/questions/1329949/photoshop-black-color-instead-of-transparency-when-pasting-from-clipboard)). That user's problem is the inverse: the clipboard stripped the alpha and revealed the hidden RGB. Both issues stem from the same root cause: alpha and RGB are independent channels, and tools handle their separation inconsistently.
The fix: flatten to opaque before re-keying
If you need to re-key an already-transparent PNG, the fix is to composite it onto an opaque background color first. This replaces every transparent pixel with a real, visible color that the scan loop can test. The steps are:
- Open the transparent PNG in any image editor.
- Create a new layer filled with the background color (white, the original green, or whatever color you want to remove).
- Flatten the image so all pixels become opaque (alpha = 255).
- Export as an opaque PNG or JPEG.
- Feed the flattened file to the green screen remover.
On our sample, flattening the already-keyed PNG onto pure white (255, 255, 255) turns all 209,102 transparent pixels into opaque white. The tool then finds zero key candidates because white is not green, which is the correct result: there is no green screen to remove. If you flatten onto the original green instead, the tool finds all 209,102 candidates and re-keys normally.
This is also the correct approach when a previous key was imperfect and you want to try again. The original opaque photograph is always the best input; a keyed RGBA PNG is a finished product, not a source file. As one graphic designer put it when discussing a related problem: "I know how to save it for web as PNG 24 with transparent background, so it can be used later on any background color. But I don't know how to save it for print" ([GD.SE 109068](https://graphicdesign.stackexchange.com/questions/109068/how-to-save-a-distressed-text-effect-logo-for-print-with-a-dark-background)). The transparent PNG is the master; the flattened export is the delivery format.
Where a re-keyed file goes next
Flattening solves the keying problem, but the flattened image is a different object from the one you started with, and that matters once it leaves your hands. If the picture came from a client, a student, or a colleague who expected their file to stay private, the re-key step is the point to check what actually left the machine.
The check is short: open the browser's request list while the file is being processed, and see whether anything with a body appears. On this tool nothing does, and the same job run with the analytics host blocked produces an identical pixel count, which is the evidence we walk through step by step.
Questions people actually ask
- What happens if I feed a PNG that already has transparency?
- Nothing: the tool skips every pixel whose alpha is zero, so zero pixels are cleared. Flatten to an opaque background first if you need to re-key.
- Why does the tool skip transparent pixels?
- The code checks
data[i+3]before calling the distance function. Alpha zero is falsy in JavaScript, so the test short-circuits and the pixel is never examined. - Does partial transparency get skipped?
- No. Only alpha equal to zero is skipped. Values from 1 to 255 are truthy and processed normally. On our sample, alpha 128 produced 209,102 cleared pixels, identical to the opaque baseline.
- What is hidden RGB?
- The color data stored under transparent pixels. Browser canvas zeroes RGB when alpha is zero, producing a black box when alpha is later dropped. Custom writers may preserve the original green, producing a green box. Flatten before export to avoid both.
- Is the tool idempotent?
- Yes. Feeding the tool's own output back as input produces zero changes because all cleared pixels have alpha equal to zero and are skipped by the scan. The file still declares the full canvas width and height, so width times height is still 268,800 and the 209,102 transparent pixels are decoded again and skipped again.
- What about an indexed-color PNG with transparency?
- One designer reported: "I have a PNG with indexed color which is rendered with a transparent background in Google Images, so I know the transparency is there, but the color mode is Indexed, and in InDesign & Photoshop I see a white, not transparent background" ([GD.SE 61248](https://graphicdesign.stackexchange.com/questions/61248/how-do-i-get-the-transparency-in-a-png-with-indexed-color-which-has-a-transparen)). Indexed PNGs use a 1-bit transparency mask, not an 8-bit alpha channel. The browser decodes them into the same RGBA array, and the tool treats fully transparent entries the same way: skipped.