How to Check a Background Removal API for Real Transparency
An output can look like a cutout on a white preview while retaining opaque background pixels. Inspect the alpha channel in a saved response and view the result on two contrasting backgrounds before accepting it.
Worked example
Local transparency check for a solid subject
from PIL import Image
with Image.open("result.png") as image:
assert image.format == "PNG"
low, high = image.convert("RGBA").getchannel("A").getextrema()
assert (low, high) == (0, 255), (low, high)
print(image.size, (low, high))Steps
- Select a test image with one solid subject, some fine edge detail and visible background. Document the expected output dimensions and transparency before using an API.
- Run the example against an already saved result.png with Pillow installed. It converts the image to RGBA, then checks for both fully transparent and fully opaque pixels on this specific test fixture.
- Overlay the result on white and dark gray. Inspect gaps, thin edges and the interior of the subject for unwanted holes or color fringing. Record failures by image.
What to check
- An opaque PNG returns (255, 255) and fails this fixture. A fully transparent PNG returns (0, 0) and also fails.
- The pixel check verifies transparency exists. Use visual review to confirm it is in the intended parts of the image.