storyboard-tui / scripts /validate_fallback_archive.py
drdavidtang's picture
Update professor voice and storyboard flow
bb94a34 verified
Raw
History Blame Contribute Delete
2.33 kB
import json
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont
ROOT = Path(__file__).resolve().parent.parent
MANIFEST = ROOT / "assets" / "fallback-manifest.json"
OUT = ROOT / "artifacts" / "fallback-ascii-contact-sheet.png"
MAX_WIDTH = 120
MAX_HEIGHT = 80
def main() -> None:
records = json.loads(MANIFEST.read_text(encoding="utf-8"))
assert len(records) == 20
font = ImageFont.load_default(size=7)
cell_width, cell_height = 720, 620
sheet = Image.new("RGB", (cell_width * 4, cell_height * 5), "#030503")
draw = ImageDraw.Draw(sheet)
results = []
for index, record in enumerate(records):
image_path = ROOT / "frontend" / "public" / record["image"].lstrip("/")
ascii_path = ROOT / record["ascii"]
assert image_path.is_file(), image_path
assert ascii_path.is_file(), ascii_path
with Image.open(image_path) as image:
assert image.size == (768, 512), (record["slug"], image.size)
art = ascii_path.read_text(encoding="utf-8")
lines = art.splitlines()
width = len(lines[0])
height = len(lines)
assert height <= MAX_HEIGHT, record["slug"]
assert width <= MAX_WIDTH, record["slug"]
assert all(len(line) == width for line in lines), record["slug"]
non_space = sum(character != " " for character in art if character != "\n")
coverage = non_space / (width * height)
unique = len(set(art) - {" ", "\n"})
assert 0.015 < coverage < 0.70, (record["slug"], coverage)
assert unique >= 3, (record["slug"], unique)
results.append((record["slug"], coverage, unique))
x = (index % 4) * cell_width + 10
y = (index // 4) * cell_height + 10
draw.text(
(x, y),
f"{index + 1:02d} {record['slug']} coverage={coverage:.1%} glyphs={unique}",
fill="#ffb547",
font=font,
)
draw.multiline_text((x, y + 22), art, fill="#78f78c", font=font, spacing=0)
OUT.parent.mkdir(parents=True, exist_ok=True)
sheet.save(OUT)
for slug, coverage, unique in results:
print(f"{slug:22} coverage={coverage:6.1%} glyphs={unique:2d}")
print(f"Validated {len(results)} individual image/ASCII pairs")
print(OUT)
if __name__ == "__main__":
main()