#!/usr/bin/env python3 """Comparison-only edge-guided Ezekiel finish-mask trial.""" from pathlib import Path import hashlib import json import subprocess 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()) FILTERED = HERE / "filtered-art.png" SMOOTHED_RIDGES = HERE / "smoothed-ridges-2000.png" PANES = REVISION / "source/pane-coverage.png" TEXT = REVISION / "source/text/text-2000.png" OVERLAY = REPO / "artifacts/layers/uncommon/v3/exports/overlay-normal-2000.png" BASELINE = REVISION / "high/normal/finish-mask.png" ART = REVISION / "source/art-master-candidate-03.png" def digest(path): return hashlib.sha256(path.read_bytes()).hexdigest() def main(): for path in (FILTERED, SMOOTHED_RIDGES, PANES, TEXT, OVERLAY, BASELINE, ART): if not path.is_file(): raise FileNotFoundError(path) edge_path = HERE / "canny-edges-2000.png" subprocess.run(["convert", str(FILTERED), "-colorspace", "Gray", "-canny", "0x1+5%+15%", str(edge_path)], check=True) edge = Image.open(edge_path).convert("L").filter(ImageFilter.MaxFilter(5)) edge_arr = np.asarray(edge, dtype=np.float32) / 255. smoothed = np.asarray(Image.open(SMOOTHED_RIDGES).convert("L"), dtype=np.float32) / 255. confidence = np.maximum(0.9 * edge_arr, 0.35 * smoothed) confidence = np.clip(confidence, 0., 1.) Image.fromarray(np.rint(confidence * 255).astype(np.uint8)).save(HERE / "edge-guided-ridges-2000.png") pane = np.asarray(Image.open(PANES).convert("L"), dtype=np.float32) / 255. overlay = np.asarray(Image.open(OVERLAY).convert("RGBA").getchannel("A"), dtype=np.float32) / 255. text = np.asarray(Image.open(TEXT).convert("RGBA").getchannel("A"), dtype=np.float32) / 255. active = 1 - (1 - overlay) * (1 - text) out = np.rint(pane * (1 - confidence) * (1 - active) * 255).astype(np.uint8) master = Image.fromarray(out) for width in (2000, 1000, 500): image = master if width == 2000 else master.resize((width, width * 7 // 5), Image.Resampling.BILINEAR) rgba = image.convert("RGBA") rgba.putalpha(255) rgba.save(HERE / f"edge-guided-normal-finish-{width}.png") baseline = Image.open(BASELINE).convert("L").resize((500, 700), Image.Resampling.BILINEAR) candidate = master.resize((500, 700), Image.Resampling.BILINEAR) art = Image.open(ART).convert("RGB").resize((500, 700), Image.Resampling.LANCZOS) sheet = Image.new("RGB", (1520, 750), "#11161c") draw = ImageDraw.Draw(sheet) font = ImageFont.truetype(str(REPO / "fonts/P052-Bold.otf"), 23) for index, (label, view) in enumerate((("Artwork", art), ("Current mask", baseline), ("Edge-guided trial", candidate))): 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-edge-guided-500.png") report = { "status": "experimental; not production-selected", "method": "Kuwahara-smoothed art, ImageMagick Canny 0x1+5%+15%, 5px max dilation; combine 90% edge confidence with 35% smoothed raw ridge confidence; original pane diagnostic and Normal overlay/text alpha retained", "sourceSHA256": {str(path.relative_to(REPO)): digest(path) for path in (ART, PANES, TEXT, OVERLAY, FILTERED, SMOOTHED_RIDGES)}, "baselineSHA256": digest(BASELINE), "outputs": {name: digest(HERE / name) for name in ("edge-guided-ridges-2000.png", "edge-guided-normal-finish-2000.png", "comparison-edge-guided-500.png")}, "warning": "This changes the approved raw-ridges-v1 extraction and is only a separate visual trial. No production or runtime files have been replaced.", } (HERE / "edge-guided-trial.json").write_text(json.dumps(report, indent=2) + "\n") if __name__ == "__main__": main()