adhoc_session.py: A reference image's alpha channel masks what until compares

For text over a moving background: only the opaque pixels count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Henrik RydgårdandClaude Opus 5.5 committed 2026-10-30 19:09:35 +01:00
1 parent 958d76ea77
commit 0e954b17cb
1 file changed
+6 -2
+6 -2
View File
@@ -396,10 +396,14 @@ def screen_matches(args, image):
want = bytes.fromhex(args.color)
have = image.convert("RGB").getpixel((x, y))
return max(abs(a - b) for a, b in zip(have, want)) <= args.tolerance
ref = Image.open(script_path(args, args.ref)).convert("RGB")
ref = Image.open(script_path(args, args.ref))
# A reference with an alpha channel is compared only where it's opaque: text over a moving
# background, say, with only the letters kept.
mask = ref.getchannel("A").point(lambda a: 255 if a >= 128 else 0) if "A" in ref.getbands() else None
ref = ref.convert("RGB")
x, y = parse_ints(args.box, 2)[:2] if args.box else (0, 0)
region = image.convert("RGB").crop((x, y, x + ref.width, y + ref.height))
diff = ImageStat.Stat(ImageChops.difference(region, ref)).mean
diff = ImageStat.Stat(ImageChops.difference(region, ref), mask).mean
return sum(diff) / len(diff) <= args.tolerance