#!/usr/bin/env python3 """Compare darkness-gated edge masks without touching production exports.""" from pathlib import Path import json import numpy as np from PIL import Image, ImageDraw, ImageFilter, ImageFont HERE = Path(__file__).resolve().parent REVISION = HERE.parents[1] REPO = next(p for p in HERE.parents if (p / "docs/card-layer-pipeline.md").is_file()) def smooth(lo, hi, value): t = np.clip((value - lo) / (hi - lo), 0, 1) return t * t * (3 - 2 * t) def output(label, confidence, pane, active): Image.fromarray(np.rint(confidence * 255).astype(np.uint8)).save(HERE / f"{label}-ridges-2000.png") result = Image.fromarray(np.rint(pane * (1 - confidence) * (1 - active) * 255).astype(np.uint8)) for width in (2000, 1000, 500): item = result if width == 2000 else result.resize((width, width * 7 // 5), Image.Resampling.BILINEAR) rgba = item.convert("RGBA") rgba.putalpha(255) rgba.save(HERE / f"{label}-normal-finish-{width}.png") return result.resize((500, 700), Image.Resampling.BILINEAR) def main(): filtered = Image.open(HERE / "filtered-art.png").convert("L") gray = np.asarray(filtered, dtype=np.float32) / 255. edge = Image.open(HERE / "canny-edges-2000.png").convert("L").filter(ImageFilter.MaxFilter(7)) edge = np.asarray(edge, dtype=np.float32) / 255. smoothed = np.asarray(Image.open(HERE / "smoothed-ridges-2000.png").convert("L"), dtype=np.float32) / 255. pane = np.asarray(Image.open(REVISION / "source/pane-coverage.png").convert("L"), dtype=np.float32) / 255. overlay = np.asarray(Image.open(REPO / "artifacts/layers/uncommon/v3/exports/overlay-normal-2000.png").getchannel("A"), dtype=np.float32) / 255. text = np.asarray(Image.open(REVISION / "source/text/text-2000.png").getchannel("A"), dtype=np.float32) / 255. active = 1 - (1 - overlay) * (1 - text) darkness = 1 - smooth(.32, .67, gray) local_max = np.asarray(filtered.filter(ImageFilter.MaxFilter(11)), dtype=np.float32) / 255. contrast = smooth(.02, .12, local_max - gray) variants = { "gated-dark": np.maximum(.25 * smoothed, .95 * edge * darkness), "gated-contrast": np.maximum(.25 * smoothed, .95 * edge * darkness * contrast), } baseline = Image.open(REVISION / "high/normal/finish-mask.png").convert("L").resize((500, 700), Image.Resampling.BILINEAR) art = Image.open(REVISION / "source/art-master-candidate-03.png").convert("RGB").resize((500, 700), Image.Resampling.LANCZOS) views = [("Artwork", art), ("Current mask", baseline)] for label, confidence in variants.items(): views.append((label.replace("-", " ").title(), output(label, np.clip(confidence, 0, 1), pane, active))) sheet = Image.new("RGB", (2030, 750), "#11161c") draw = ImageDraw.Draw(sheet) font = ImageFont.truetype(str(REPO / "fonts/P052-Bold.otf"), 23) for index, (label, view) in enumerate(views): x = 10 + index * 505 draw.text((x + 6, 8), label, fill="#f4ead4", font=font) sheet.paste(view.convert("RGB"), (x, 40)) sheet.save(HERE / "comparison-gated-500.png") (HERE / "gated-trial.json").write_text(json.dumps({ "status": "experimental; not production-selected", "method": "Canny near-edge mask with dark-luminance gating, optionally local dark-line contrast gating, plus 25% Kuwahara-smoothed raw ridge confidence; original pane diagnostic and Normal overlay/text alpha retained", "parameters": {"canny": "0x1+5%+15%", "edgeDilation": 7, "darknessSmoothstep": [.32, .67], "localMaxSize": 11, "contrastSmoothstep": [.02, .12]}, "warning": "These masks are comparison-only and have not been validated under moving light or installed in any printing." }, indent=2) + "\n") if __name__ == "__main__": main()