render_external_anchor_label_crops_018.py VOYNICH RESEARCH ARCHIVE | 16 SEPTEMBER 2026 2 if best is None or ratio>best[0]:best=(ratio,tj,ln,tline) if not best or best[0]<.5:continue _,tj,ln,tline=best;used.add(tj);sm=SequenceMatcher(a=[collapse(x['word']) for x in vline],b=[collapse(x) for x in tline],autojunk=False) for a,b,n in sm.get_matching_blocks(): for j in range(n):out[(fol,str(ln),str(b+j))]=vline[a+j] return out ranking=list(csv.DictReader((OUT/'frozen_candidate_ranking.csv').open())) strict={r['payload']:r['stem_id'] for r in ranking if r['strict_candidate_gate']=='1'} events=[r for r in csv.DictReader((OUT/'label_prose_events.csv').open()) if r['witness']=='ZL' and r['register']=='LABEL' and r['payload'] in strict] bmap=aligned_label_boxes() doc=fitz.open(PDF); pm=pagemap(); tiles=[]; font=ImageFont.load_default() for e in events: k=(e['folio'],e['line'],e['token_index']); b=bmap.get(k); bf=base(e['folio']) if not b or bf not in pm:continue pix=doc[pm[bf]].get_pixmap(matrix=fitz.Matrix(2.0,2.0),alpha=False) im=Image.frombytes('RGB',(pix.width,pix.height),pix.samples) sx=im.width/600;sy=im.height/837;x=float(b['x']);y=float(b['y']);w=float(b['w']);h=float(b['h']) # Wide context is deliberate: labels often point into diagrams/plants rather than sit inside them. x0=max(0,int((x-150)*sx));x1=min(im.width,int((x+w+150)*sx));y0=max(0,int((y-120)*sy));y1=min(im.height ,int((y+h+120)*sy)) crop=im.crop((x0,y0,x1,y1));crop.thumbnail((500,360)) tile=Image.new('RGB',(520,410),'white');tile.paste(crop,((520-crop.width)//2,42));d=ImageDraw.Draw(tile ) d.text((8,8),f"{strict[e['payload']]} [{e['payload']}] {e['folio']}.{e['line']} token {e['token_index']}",fill='black',font=font) tiles.append(tile) cols=2;rows=max(1,math.ceil(len(tiles)/cols));sheet=Image.new('RGB',(cols*520,rows*410),(225,229,233)) for i,t in enumerate(tiles):sheet.paste(t,((i%cols)*520,(i//cols)*410)) sheet.save(OUT/'strict_candidate_label_coordinate_reveal.jpg',quality=90) print({'strict_candidates':strict,'label_events':len(events),'matched_crops':len(tiles)})