Most "AI wrote my tool" posts show the happy path. This one shows the prompt, the script a coding agent produced from it, and the two things the first version got wrong — because the failures are the reusable part.
The task
I had a folder of background-removed product PNGs and no reliable way to tell which ones would look bad outside a white page. I wanted a script that reports four numbers per image:
- how many edge-band pixels exist (0 < alpha < 250);
- the mean and max colour delta between a white composite and a near-black composite;
- the share of edge pixels that read light on white;
- the count of non-opaque pixels enclosed by the subject (holes).
The prompt
Five constraint lines did the work. Everything else was context.
Write a single-file Python script using only Pillow and the standard library.
Input: a folder of RGBA PNGs. Output: one CSV row per image.
Constraints:
1. Composite each image over white (255) and over (16,16,16). Report mean and max
per-channel delta for pixels with 0 < alpha < 250 only.
2. "Enclosed" means a non-opaque pixel not reachable from the border through
4-connected non-opaque pixels. Use an explicit flood fill, not PIL heuristics.
3. Do not downscale, do not convert colour spaces, do not auto-correct.
4. If an image has no pixels with 0 < alpha < 250, write zeros, not NaN.
5. Print progress to stderr only; stdout is the CSV.
Why each line matters: (1) fixes the measurement band, (2) prevents the classic wrong answer — getbbox() on the alpha channel does not find holes, (3) removes the "helpful" preprocessing that changes the numbers, (4) avoids a crash on hard-matted images, (5) keeps the output pipeable.
What the agent got wrong
The first draft used alpha < 255 as the edge band. That includes fully transparent background pixels, which are identical in both composites and inflate the pixel count by two orders of magnitude. The number looked plausible and meant nothing.
The second draft computed "enclosed" by checking whether a non-opaque pixel had opaque neighbours on all four sides. That finds one-pixel pinholes, not the 30×40 region punched through a subject's interior. The constraint had to be restated as an explicit border flood fill before the answer was right.
Both failures are the same shape: the agent optimized for a short script, and the shortest script was wrong in a way that produced clean-looking output.
The corrected core
from collections import deque
from PIL import Image
def audit(path, dark=(16, 16, 16)):
img = Image.open(path).convert("RGBA")
w, h = img.size
px = img.load()
band = total = 0
peak = 0.0
for y in range(h):
for x in range(w):
r, g, b, a = px[x, y]
if 0 < a < 250:
band += 1
aw = a / 255.0
for i, c in enumerate((r, g, b)):
on_w = c * aw + 255 * (1 - aw)
on_d = c * aw + dark[i] * (1 - aw)
d = abs(on_w - on_d)
total += d
peak = max(peak, d)
outside = bytearray(w * h)
q = deque()
for x in range(w):
for y in (0, h - 1):
if px[x, y][3] < 250 and not outside[y * w + x]:
outside[y * w + x] = 1; q.append((x, y))
for y in range(h):
for x in (0, w - 1):
if px[x, y][3] < 250 and not outside[y * w + x]:
outside[y * w + x] = 1; q.append((x, y))
while q:
x, y = q.popleft()
for nx, ny in ((x-1,y),(x+1,y),(x,y-1),(x,y+1)):
if 0 <= nx < w and 0 <= ny < h and not outside[ny * w + nx] and px[nx, ny][3] < 250:
outside[ny * w + nx] = 1; q.append((nx, ny))
holes = sum(1 for y in range(h) for x in range(w)
if px[x, y][3] < 250 and not outside[y * w + x])
return band, (total / max(band * 3, 1)), peak, holes
On a synthetic 600×600 test image with a 1.5px feather, this reported 4,000+ band pixels, a mean delta of about 15/255, and zero holes. Punching a 40×30 transparent rectangle through the subject moved the hole count to the expected 1,200 pixels — the check that the earlier getbbox() version silently missed.
What to reuse
- Put the measurement band in the prompt, not in a follow-up. Agents default to
alpha < 255. - Define geometric predicates operationally ("reachable from the border by 4-connected non-opaque pixels"), not by intent ("holes").
- Ask for zeros instead of NaN on empty inputs, and CSV on stdout. Small constraints, big reduction in post-processing.
- Verify the script on a synthetic image with a known defect before trusting it on real files.
The prompt is portable: swap the four metrics and the same five lines work for any per-pixel audit.
I used this audit while building CleanWhiteBG, a background remover for product photos.
Top comments (0)