"""Compose the rendered variants into one labelled sheet. python3 contact_sheet.py out/ """ import json import os import sys from PIL import Image, ImageDraw, ImageFont FONT = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf" FONT_BOLD = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" PANEL_W = 820 PAD = 18 TITLE_H = 74 def main(out_dir): with open(os.path.join(out_dir, "summary.json")) as f: summary = json.load(f) with open(os.path.join(out_dir, "verify.json")) as f: verified = json.load(f) names = list(summary["variants"]) panels = [] for name in names: img = Image.open(os.path.join(out_dir, f"preview_{name}.png")) ratio = PANEL_W / img.width panels.append(img.resize((PANEL_W, int(img.height * ratio)), Image.LANCZOS)) panel_h = panels[0].height label_h = 96 width = PANEL_W * len(panels) + PAD * (len(panels) + 1) height = TITLE_H + panel_h + label_h + PAD sheet = Image.new("RGB", (width, height), (32, 34, 36)) draw = ImageDraw.Draw(sheet) title = ImageFont.truetype(FONT_BOLD, 30) body = ImageFont.truetype(FONT, 20) small = ImageFont.truetype(FONT, 17) draw.text((PAD, 20), f"Rice cube — {summary['cube_mm']:.0f} mm rounded cube, " f"displacement from {summary['height_map']}", font=title, fill=(235, 235, 235)) draw.text((PAD, 52), f"source: {summary['source']} · one height field at three " f"apparent grain scales · relief = " f"{summary['relief_ratio']:.2f} x grain (from the map itself)", font=small, fill=(150, 152, 155)) for index, (name, panel) in enumerate(zip(names, panels)): x = PAD + index * (PANEL_W + PAD) sheet.paste(panel, (x, TITLE_H)) info = summary["variants"][name] check = verified.get(f"rice_cube_{name}.3mf", {}) lines = [ f"{name} {info['grain_mm']} mm grains", f"texture spans {info['patch_mm']:.0f} mm " f"relief {info['displacement_mm']:.2f} mm", f"{check.get('triangles', 0):,} tris " f"{check.get('signed_volume_mm3', 0):,.0f} mm3 " f"boundary edges {check.get('boundary_edges', '?')}", ] for row, text in enumerate(lines): draw.text((x + 4, TITLE_H + panel_h + 10 + row * 27), text, font=body if row == 0 else small, fill=(232, 232, 232) if row == 0 else (158, 160, 163)) path = os.path.join(out_dir, "variants.png") sheet.save(path) print(f"wrote {path} ({sheet.width}x{sheet.height})") if __name__ == "__main__": main(sys.argv[1] if len(sys.argv) > 1 else "out")