Files

116 lines
4.6 KiB
Python

#!/usr/bin/env python3
"""Verify exported Isaiah printings against declared components."""
import json
from pathlib import Path
import numpy as np
from PIL import Image, ImageDraw, ImageFont
ROOT = Path(__file__).resolve().parents[1]
REPO = next(p for p in ROOT.parents if (p / "docs/card-layer-pipeline.md").is_file())
card = json.loads((ROOT / "card.json").read_text())
art = Image.open(REPO / card["art"]).convert("RGBA")
text = Image.open(ROOT / "source/text/text-2000.png").convert("RGBA")
layers = REPO / "artifacts/layers/rare/v3/exports"
printings = {
"normal": ("overlay-normal", True),
"boundless": (None, False),
"borderless": ("backing-borderless", True),
"textless": ("frame-textless", False),
}
checks = []
for printing, (component, has_text) in printings.items():
overlay = (
Image.open(layers / f"{component}-2000.png").convert("RGBA")
if component
else None
)
expected = Image.alpha_composite(art, overlay) if overlay else art.copy()
if has_text:
expected = Image.alpha_composite(expected, text)
for resolution, width in [("low", 500), ("med", 1000), ("high", 2000)]:
dims = (width, width * 7 // 5)
folder = ROOT / resolution / printing
for filename in ["card.png", "finish-mask.png", "text-mask.png"]:
image = Image.open(folder / filename).convert("RGBA")
assert image.size == dims, (folder, filename, "dimensions")
assert image.getchannel("A").getextrema() == (255, 255), (
folder,
filename,
"opacity",
)
pixels = np.asarray(image)
if filename != "card.png":
assert np.array_equal(pixels[:, :, 0], pixels[:, :, 1])
assert np.array_equal(pixels[:, :, 1], pixels[:, :, 2])
else:
resized = expected.resize(dims, Image.Resampling.LANCZOS)
assert np.array_equal(pixels, np.asarray(resized)), "Composition mismatch"
if filename == "text-mask.png" and not has_text:
assert image.getchannel("R").getextrema() == (0, 0)
checks.append(
{
"file": str((folder / filename).relative_to(ROOT)),
"dimensions": list(dims),
"pass": True,
}
)
finish = np.asarray(
Image.open(ROOT / "high" / printing / "finish-mask.png").getchannel("R")
)
if overlay:
assert np.all(finish[np.asarray(overlay.getchannel("A")) == 255] == 0)
if not has_text:
assert np.count_nonzero(finish[2250:2650, 200:1800]) > 0
for kind in ["finish-mask", "text-mask"]:
board = Image.new("RGB", (1200, 480), "#11161c")
draw = ImageDraw.Draw(board)
font = ImageFont.truetype(str(REPO / "fonts/P052-Bold.otf"), 22)
for index, printing in enumerate(printings):
draw.text((300 * index + 10, 8), printing.title(), fill="#f4ead4", font=font)
image = Image.open(ROOT / "med" / printing / f"{kind}.png").convert("RGB")
board.paste(
image.resize((280, 392), Image.Resampling.BILINEAR),
(300 * index + 10, 50),
)
board.save(ROOT / "review" / f"{kind}s-comparison.png")
normal = Image.open(ROOT / "high/normal/card.png")
normal.crop((0, 0, 2000, 380)).save(ROOT / "review/normal-header-master.png")
normal.crop((0, 2160, 2000, 2800)).save(ROOT / "review/normal-footer-master.png")
report = {
"status": "passed",
"filesChecked": len(checks),
"checks": checks,
"compositionPixelsMatchDeclaredComponents": True,
"boundlessMatchesApprovedMaster": True,
"opaqueOverlaysHaveZeroFinish": True,
"textlessAndBoundlessTextMasksAreZero": True,
"removedBackingsRecoverFinish": True,
"movingLight": "not performed",
"harnessFixturesInstalled": False,
}
(ROOT / "review/assembly-validation.json").write_text(
json.dumps(report, indent=2) + "\n"
)
build = json.loads((ROOT / "review/build-validation.json").read_text())
guide = Image.open(ROOT / "high/normal/card.png").convert("RGB")
draw = ImageDraw.Draw(guide)
for glyph in build["typography"]["glyphs"]:
draw.rectangle(glyph["unclippedInkBounds"], outline="#d52438", width=3)
draw.line((140, glyph["baseline"], 1860, glyph["baseline"]), fill="#2479dd", width=2)
line_top, line_bottom = build["typography"]["referenceLineBox"]
draw.rectangle((160, line_top, 1840, line_bottom), outline="#199047", width=3)
guide.resize((1000, 1400), Image.Resampling.LANCZOS).save(
ROOT / "review/typography-guide.png"
)
print(
"Passed: 36 PNGs; decoded printing composition, mask conventions, "
"dimensions and opacity."
)