200 lines
8.2 KiB
Python
200 lines
8.2 KiB
Python
"""Render flattened PSD references beside the semantic marker layer.
|
|
|
|
The PSD merged preview is the saved composite of every visible layer. The
|
|
right-hand audit image uses the marker-free base raster plus the extracted
|
|
semantic markers, making missing, duplicated and off-map records easy to spot.
|
|
|
|
python tools/render_marker_audit.py --source-root C:/path/to/checkout
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
from psd_tools import PSDImage
|
|
|
|
from common import DATA, ROOT, find_map, load_config
|
|
|
|
MAX_PANEL = (1500, 1050)
|
|
EXTRACT_CATEGORIES = {
|
|
"extract-pmc", "extract-scav", "extract-neutral", "extract-hidden", "transit",
|
|
}
|
|
|
|
|
|
def font(size: int, bold: bool = False):
|
|
choices = [
|
|
Path("C:/Windows/Fonts/seguisb.ttf" if bold else "C:/Windows/Fonts/segoeui.ttf"),
|
|
Path("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" if bold
|
|
else "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"),
|
|
]
|
|
for path in choices:
|
|
if path.exists():
|
|
return ImageFont.truetype(str(path), size)
|
|
return ImageFont.load_default()
|
|
|
|
|
|
def flattened(psd: PSDImage) -> Image.Image:
|
|
image = psd.topil()
|
|
if image is None:
|
|
image = psd.composite()
|
|
if image is None:
|
|
raise RuntimeError("PSD has neither a merged preview nor a compositable image")
|
|
return image.convert("RGBA")
|
|
|
|
|
|
def on_dark(image: Image.Image) -> Image.Image:
|
|
result = Image.new("RGB", image.size, "#171a1f")
|
|
if image.mode != "RGBA":
|
|
image = image.convert("RGBA")
|
|
result.paste(image, mask=image.getchannel("A"))
|
|
return result
|
|
|
|
|
|
def fit(image: Image.Image, size=MAX_PANEL) -> Image.Image:
|
|
image = image.copy()
|
|
image.thumbnail(size, Image.Resampling.LANCZOS)
|
|
return image
|
|
|
|
|
|
def merged_markers() -> list[dict]:
|
|
markers = []
|
|
marker_dir = DATA / "markers"
|
|
edit_dir = DATA / "marker-edits"
|
|
for marker_path in sorted(marker_dir.glob("*.json")):
|
|
raw = json.loads(marker_path.read_text(encoding="utf-8"))
|
|
edits_path = edit_dir / marker_path.name
|
|
edits = json.loads(edits_path.read_text(encoding="utf-8")) \
|
|
if edits_path.exists() else {}
|
|
overrides = edits.get("overrides", {})
|
|
deleted = set(edits.get("deleted", []))
|
|
selected = [{**item, **overrides.get(item["id"], {})}
|
|
for item in raw.get("markers", []) if item["id"] not in deleted]
|
|
ids = {item["id"] for item in selected}
|
|
selected.extend(item for item in edits.get("added", []) if item.get("id") not in ids)
|
|
markers.extend(selected)
|
|
return markers
|
|
|
|
|
|
def marker_overlay(base: Image.Image, markers: list[dict], config: dict) -> Image.Image:
|
|
image = fit(on_dark(base.convert("RGBA")))
|
|
draw = ImageDraw.Draw(image)
|
|
sx, sy = image.width / base.width, image.height / base.height
|
|
categories = config.get("categories", {})
|
|
label_font = font(13, True)
|
|
small_font = font(11, True)
|
|
for marker in markers:
|
|
category = marker.get("category", "poi")
|
|
settings = categories.get(category, {})
|
|
x = float(marker["x"]) * base.width * sx
|
|
y = float(marker["y"]) * base.height * sy
|
|
extract = category in EXTRACT_CATEGORIES
|
|
radius = 10 if extract else max(4, min(8, int(settings.get("size", 16) * 0.38)))
|
|
color = settings.get("color", "#6c7682")
|
|
outline = "#ffffff" if extract else "#e8edf2"
|
|
draw.ellipse((x - radius, y - radius, x + radius, y + radius), fill=color,
|
|
outline=outline, width=3 if extract else 2)
|
|
symbol = str(settings.get("symbol", "•"))
|
|
if symbol.isascii() and len(symbol) <= 2:
|
|
box = draw.textbbox((0, 0), symbol, font=small_font)
|
|
draw.text((x - (box[2] - box[0]) / 2, y - (box[3] - box[1]) / 2 - 1),
|
|
symbol, fill="white", font=small_font)
|
|
if extract:
|
|
label = str(marker.get("label") or settings.get("label") or category)
|
|
box = draw.textbbox((0, 0), label, font=label_font)
|
|
tx, ty = x + radius + 4, y - (box[3] - box[1]) / 2
|
|
draw.rounded_rectangle((tx - 3, ty - 2, tx + box[2] - box[0] + 3,
|
|
ty + box[3] - box[1] + 3), radius=3, fill="#0c1115e6")
|
|
draw.text((tx, ty), label, fill="white", font=label_font)
|
|
return image
|
|
|
|
|
|
def full_reference(image: Image.Image, crop: tuple[int, int, int, int]) -> Image.Image:
|
|
full = fit(on_dark(image), (1600, 1200))
|
|
draw = ImageDraw.Draw(full)
|
|
sx, sy = full.width / image.width, full.height / image.height
|
|
left, top, right, bottom = crop
|
|
draw.rectangle((left * sx, top * sy, right * sx, bottom * sy), outline="#ffcf4a", width=3)
|
|
draw.text((12, 10), "Yellow rectangle = configured map crop", fill="#ffcf4a",
|
|
font=font(16, True), stroke_width=2, stroke_fill="#111111")
|
|
return full
|
|
|
|
|
|
def comparison(map_name: str, reference: Image.Image, semantic: Image.Image,
|
|
count: int, extract_count: int) -> Image.Image:
|
|
header = 54
|
|
gap = 12
|
|
width = reference.width + semantic.width + gap
|
|
height = max(reference.height, semantic.height) + header
|
|
canvas = Image.new("RGB", (width, height), "#101317")
|
|
canvas.paste(reference, (0, header))
|
|
canvas.paste(semantic, (reference.width + gap, header))
|
|
draw = ImageDraw.Draw(canvas)
|
|
draw.text((10, 8), f"{map_name}: flattened visible PSD", fill="white", font=font(18, True))
|
|
draw.text((reference.width + gap + 10, 8),
|
|
f"semantic layer: {count} markers / {extract_count} extracts",
|
|
fill="white", font=font(18, True))
|
|
return canvas
|
|
|
|
|
|
def make_contact_sheet(paths: list[Path], output: Path) -> None:
|
|
thumbs = []
|
|
for path in paths:
|
|
with Image.open(path) as image:
|
|
thumbs.append(fit(image.convert("RGB"), (760, 430)))
|
|
cols = 2
|
|
rows = (len(thumbs) + cols - 1) // cols
|
|
sheet = Image.new("RGB", (cols * 770, rows * 440), "#0d1014")
|
|
for index, image in enumerate(thumbs):
|
|
sheet.paste(image, ((index % cols) * 770, (index // cols) * 440))
|
|
sheet.save(output, "JPEG", quality=90, optimize=True)
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("ids", nargs="*")
|
|
parser.add_argument("--source-root", type=Path)
|
|
parser.add_argument("--output", type=Path, default=ROOT / "build" / "marker-audit")
|
|
args = parser.parse_args()
|
|
cfg = load_config()
|
|
maps = [find_map(cfg, item) for item in args.ids] if args.ids else cfg.maps
|
|
maps = [item for item in maps if not item.get("base")]
|
|
markers = merged_markers()
|
|
marker_config = json.loads((DATA / "marker-config.json").read_text(encoding="utf-8"))
|
|
args.output.mkdir(parents=True, exist_ok=True)
|
|
comparisons = []
|
|
|
|
for item in maps:
|
|
local = ROOT / item["psd"]
|
|
source = local if local.exists() else (args.source_root / item["psd"] if args.source_root else local)
|
|
print(f"[{item['id']}] flattening {source}", flush=True)
|
|
psd = PSDImage.open(source)
|
|
image = flattened(psd)
|
|
crop = tuple(map(int, item.get("crop") or (0, 0, psd.width, psd.height)))
|
|
cropped_source = image.crop(crop)
|
|
cropped = fit(on_dark(cropped_source))
|
|
full = full_reference(image, crop)
|
|
full.save(args.output / f"{item['id']}-visible-full.jpg", "JPEG", quality=91)
|
|
|
|
# Keep both audit panes on the exact same flattened PSD crop. Generated
|
|
# build images can be stale after crop or alignment edits.
|
|
selected = [marker for marker in markers if marker.get("mapId") == item["id"]]
|
|
semantic = fit(marker_overlay(cropped_source, selected, marker_config))
|
|
extracts = sum(marker.get("category") in EXTRACT_CATEGORIES for marker in selected)
|
|
result = comparison(item.get("name", item["id"]), cropped, semantic,
|
|
len(selected), extracts)
|
|
path = args.output / f"{item['id']}-comparison.jpg"
|
|
result.save(path, "JPEG", quality=92)
|
|
comparisons.append(path)
|
|
print(f"[{item['id']}] wrote {path.name}", flush=True)
|
|
del psd, image, cropped, full, semantic, result
|
|
|
|
make_contact_sheet(comparisons, args.output / "all-comparisons.jpg")
|
|
print(f"wrote {args.output / 'all-comparisons.jpg'}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|