from pathlib import Path import fitz from PIL import Image pdf = Path(__file__).parent.parent / "raw_pdfs" / "355 E 86th St Mechanical 100% CD Set_flattened.pdf" doc = fitz.open(pdf) page = doc.load_page(1) # page 2 pix = page.get_pixmap(matrix=fitz.Matrix(300/72, 300/72), alpha=False) img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples) W, H = img.width, img.height print(f"Image size at 300 DPI: {W} x {H}") GOLD_ACP = [ {"tag": "AC-P", "x": 7440, "y": 1650, "w": 320, "h": 430}, {"tag": "AC-P", "x": 2730, "y": 3810, "w": 839, "h": 570}, {"tag": "AC-P", "x": 2730, "y": 4770, "w": 869, "h": 570}, {"tag": "AC-P", "x": 5220, "y": 4680, "w": 570, "h": 300}, ] print("Gold box positions as % of image:") for g in GOLD_ACP: pct_x = g["x"] / W * 100 pct_y = g["y"] / H * 100 print(f" ({g['x']},{g['y']}) => ({pct_x:.1f}%, {pct_y:.1f}%)") # Draw gold boxes on a small preview and save from PIL import ImageDraw preview = img.resize((1200, int(1200 * H / W)), Image.LANCZOS) scale = 1200 / W draw = ImageDraw.Draw(preview) for g in GOLD_ACP: x1 = int(g["x"] * scale) y1 = int(g["y"] * scale) x2 = int((g["x"] + g["w"]) * scale) y2 = int((g["y"] + g["h"]) * scale) draw.rectangle([x1, y1, x2, y2], outline=(0, 255, 0), width=4) preview.save(Path(__file__).parent / "output_floor_iter" / "model_compare_acp" / "gold_boxes_preview.png") print("Saved gold_boxes_preview.png")