dougvk/Unlimited-OCR-RDNA4
0
1#!/usr/bin/env python32from __future__ import annotations3 4import argparse5from pathlib import Path6 7from PIL import Image, ImageDraw, ImageFont8 9 10def resolve_font_pair(regular_override: Path | None, bold_override: Path | None) -> tuple[Path, Path]:11 if (regular_override is None) != (bold_override is None):12 raise SystemExit("--regular-font and --bold-font must be supplied together")13 if regular_override and bold_override:14 if not regular_override.is_file() or not bold_override.is_file():15 raise SystemExit("one or both explicit font paths do not exist")16 return regular_override, bold_override17 18 families = (19 ("DejaVuSans.ttf", "DejaVuSans-Bold.ttf"),20 ("LiberationSans-Regular.ttf", "LiberationSans-Bold.ttf"),21 )22 roots = (Path("/usr/share/fonts"), Path("/usr/local/share/fonts"))23 for regular_name, bold_name in families:24 for root in roots:25 regular_matches = sorted(root.rglob(regular_name)) if root.is_dir() else []26 bold_matches = sorted(root.rglob(bold_name)) if root.is_dir() else []27 if regular_matches and bold_matches:28 return regular_matches[0], bold_matches[0]29 raise SystemExit("no DejaVu Sans or Liberation Sans font pair found; pass --regular-font and --bold-font")30 31 32def main() -> None:33 parser = argparse.ArgumentParser(description="Generate a deterministic OCR validation page")34 parser.add_argument("--output", type=Path, required=True)35 parser.add_argument("--pdf-output", type=Path, help="also write the page as a 150-DPI PDF")36 parser.add_argument("--regular-font", type=Path, help="regular TrueType/OpenType font override")37 parser.add_argument("--bold-font", type=Path, help="bold TrueType/OpenType font override")38 args = parser.parse_args()39 40 regular_path, bold_path = resolve_font_pair(args.regular_font, args.bold_font)41 image = Image.new("RGB", (1600, 2000), "white")42 draw = ImageDraw.Draw(image)43 title = ImageFont.truetype(str(bold_path), 68)44 heading = ImageFont.truetype(str(bold_path), 38)45 body = ImageFont.truetype(str(regular_path), 34)46 small = ImageFont.truetype(str(regular_path), 28)47 48 draw.text((120, 100), "Unlimited OCR RDNA4 Smoke Test", font=title, fill="black")49 draw.line((120, 205, 1480, 205), fill="black", width=4)50 draw.text((120, 265), "Invoice No. OCR-2026-0731", font=body, fill="black")51 draw.text((120, 325), "Date: 31 July 2026", font=body, fill="black")52 draw.text((120, 385), "GPU target: AMD gfx1201", font=body, fill="black")53 54 draw.text((120, 505), "Items", font=heading, fill="black")55 left, top, right, bottom = 120, 580, 1480, 105056 columns = (left, 930, 1120, right)57 rows = (top, 690, 810, 930, bottom)58 for x in columns:59 draw.line((x, top, x, bottom), fill="black", width=3)60 for y in rows:61 draw.line((left, y, right, y), fill="black", width=3)62 draw.text((145, 610), "Item", font=heading, fill="black")63 draw.text((960, 610), "Qty", font=heading, fill="black")64 draw.text((1150, 610), "Price", font=heading, fill="black")65 draw.text((145, 725), "Document scan", font=body, fill="black")66 draw.text((980, 725), "2", font=body, fill="black")67 draw.text((1150, 725), "€19.50", font=body, fill="black")68 draw.text((145, 845), "Table extraction", font=body, fill="black")69 draw.text((980, 845), "1", font=body, fill="black")70 draw.text((1150, 845), "€9.50", font=body, fill="black")71 draw.text((145, 965), "Total", font=heading, fill="black")72 draw.text((1150, 965), "€48.50", font=heading, fill="black")73 74 draw.text((120, 1190), "Verification notes", font=heading, fill="black")75 notes = [76 "• AMD Radeon RX 9070 XT",77 "• ROCm architecture: gfx1201",78 "• Expected checksum: RDNA4-OCR-PASS",79 "• Formula sample: E = mc²",80 ]81 for index, note in enumerate(notes):82 draw.text((150, 1270 + index * 70), note, font=body, fill="black")83 84 draw.line((120, 1780, 1480, 1780), fill="black", width=2)85 draw.text((120, 1820), "Synthetic validation page; no private data.", font=small, fill="black")86 args.output.parent.mkdir(parents=True, exist_ok=True)87 image.save(args.output, format="PNG", optimize=True)88 print(args.output)89 90 if args.pdf_output:91 args.pdf_output.parent.mkdir(parents=True, exist_ok=True)92 image.save(args.pdf_output, format="PDF", resolution=150.0)93 print(args.pdf_output)94 95 96if __name__ == "__main__":97 main()98 