import warnings, json warnings.filterwarnings('ignore') from PIL import Image, ImageDraw, ImageFont Image.MAX_IMAGE_PIXELS = None import fitz with open('output/floor_002_Cellar_detections.json') as f: hits = json.load(f)['hits'] print('Detections:') for h in hits: print(f" {h['tag']}: ({h['x']},{h['y']}) {h['w']}x{h['h']}") pdf = fitz.open(r'..\raw_pdfs\355 E 86th St Mechanical 100% CD Set_flattened.pdf') page = pdf[1] mat = fitz.Matrix(300/72, 300/72) pix = page.get_pixmap(matrix=mat, alpha=False) img = Image.frombytes('RGB', [pix.width, pix.height], pix.samples) pdf.close() draw = ImageDraw.Draw(img) try: font = ImageFont.truetype('arial.ttf', 40) except: font = ImageFont.load_default() colors = {'AC-P':(0,180,0),'AC-T':(0,0,255),'AC-U':(180,0,180), 'AC-S':(255,140,0),'AC-Q':(0,180,180),'AC-D':(255,0,0)} for h in hits: x,y,w,ht,tag = h['x'],h['y'],h['w'],h['h'],h['tag'] color = colors.get(tag,(0,0,0)) draw.rectangle([x, y, x+w, y+ht], outline=color, width=6) draw.text((x, y-45), tag, fill=color, font=font) # Zoom into upper-mid cluster crop = img.crop((2000, 1000, 7500, 5000)) crop = crop.resize((1800, int(1800*crop.size[1]/crop.size[0])), Image.LANCZOS) crop.save('output/cellar_refined_zoom.png') print('Saved zoom', crop.size)