Create a labelled image of give image with correct labellings
cranial fossa anatomy
cranial fossae anatomy anterior middle posterior skull base
/tmp/workspace
mkdir -p "/tmp/workspace/cranial-cavity-labelled/output" python - <<'PY' from PIL import Image, ImageDraw, ImageFont from pathlib import Path src=Path('/tmp/workspace/attachments/38869b3e-17b0-481d-8185-a28d1477fd4c/198962.jpg') out=Path('/tmp/workspace/cranial-cavity-labelled/output/labelled_cranial_cavity.jpg') im=Image.open(src).convert('RGB') # resize slight for annotation room im=im.resize((960,1280), Image.Resampling.LANCZOS) canvas=Image.new('RGB',(1500,1280),'white') canvas.paste(im,(0,0)) d=ImageDraw.Draw(canvas) try: font=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',27) small=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',20) except: font=ImageFont.load_default(); small=font # labeled callouts: anchor target then text left/right and y items=[ ('Frontal bone\n(internal surface)',(475,150),(1005,95),'right'), ('Anterior cranial fossa',(485,305),(1005,220),'right'), ('Meningeal arterial\nbranches',(130,500),(42,505),'left'), ('Sphenoid bone\n(central skull base)',(525,455),(1000,390),'right'), ('Middle cranial fossa',(760,600),(1005,560),'right'), ('Petrous part of\ntemporal bone',(700,700),(1005,720),'right'), ('Cerebral hemisphere\n(brain model)',(445,740),(28,800),'left'), ('Cortical arteries\n(painted red)',(520,825),(1000,880),'right'), ('Posterior cranial fossa\n(partly obscured)',(470,1005),(1000,1050),'right'), ] line=(14,75,132); box=(232,244,255) for text,pt,pos,side in items: # draw leader end outside label tx,ty=pos # determine box sizing bbox=d.multiline_textbbox((0,0),text,font=font,spacing=4) w=bbox[2]-bbox[0];h=bbox[3]-bbox[1] pad=12 x0=tx; y0=ty x1=x0+w+2*pad; y1=y0+h+2*pad d.rounded_rectangle((x0,y0,x1,y1),radius=10,fill=box,outline=line,width=3) d.multiline_text((x0+pad,y0+pad-2),text,font=font,fill=(4,38,75),spacing=4) # connect point to nearest edge of box, with elbow edge=(x0 if side=='right' else x1, (y0+y1)//2) elbow=(edge[0]-35 if side=='right' else edge[0]+35, edge[1]) d.line([pt,(elbow[0],pt[1]),edge],fill=line,width=4) r=7 d.ellipse((pt[0]-r,pt[1]-r,pt[0]+r,pt[1]+r),fill=(220,45,55),outline='white',width=2) # Header/footer d.rectangle((0,0,1500,57),fill=(5,47,87)) d.text((18,13),'Cranial cavity - superior view (anterior at top)',font=font,fill='white') d.rectangle((965,1180,1499,1279),fill=(245,248,251),outline=(150,170,190)) foot='Labels identify gross features visible\nin this teaching model.\nRed lines are painted arteries.' d.multiline_text((985,1200),foot,font=small,fill=(45,60,72),spacing=5) canvas.save(out,quality=95) print(out, canvas.size) PY
~/cranial-cavity-labelled/output
~/cranial-cavity-labelled/annotate.py
from PIL import Image, ImageDraw, ImageFont
from pathlib import Path
src = Path('/tmp/workspace/attachments/38869b3e-17b0-481d-8185-a28d1477fd4c/198962.jpg')
out = Path('/tmp/workspace/cranial-cavity-labelled/output/labelled_cranial_cavity.jpg')
out.parent.mkdir(parents=True, exist_ok=True)
im = Image.open(src).convert('RGB').resize((960, 1280), Image.Resampling.LANCZOS)
canvas = Image.new('RGB', (1500, 1280), 'white')
canvas.paste(im, (0, 0))
d = ImageDraw.Draw(canvas)
font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf', 27)
small = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 20)
items = [
('Frontal bone\n(internal surface)', (475, 150), (1005, 95), 'right'),
('Anterior cranial fossa', (485, 305), (1005, 220), 'right'),
('Meningeal arterial\nbranches', (130, 500), (25, 505), 'left'),
('Sphenoid bone\n(central skull base)', (525, 455), (1000, 390), 'right'),
('Middle cranial fossa', (760, 600), (1005, 560), 'right'),
('Petrous part of\ntemporal bone', (700, 700), (1005, 720), 'right'),
('Cerebral hemisphere\n(brain model)', (445, 740), (22, 800), 'left'),
('Cortical arteries\n(painted red)', (520, 825), (1000, 880), 'right'),
('Posterior cranial fossa\n(partly obscured)', (470, 1005), (1000, 1050), 'right'),
]
line = (14, 75, 132)
box = (232, 244, 255)
for text, pt, pos, side in items:
tx, ty = pos
bbox = d.multiline_textbbox((0, 0), text, font=font, spacing=4)
w, h = bbox[2] - bbox[0], bbox[3] - bbox[1]
pad = 12
x0, y0, x1, y1 = tx, ty, tx + w + 2 * pad, ty + h + 2 * pad
d.rounded_rectangle((x0, y0, x1, y1), radius=10, fill=box, outline=line, width=3)
d.multiline_text((x0 + pad, y0 + pad - 2), text, font=font, fill=(4, 38, 75), spacing=4)
edge = (x0 if side == 'right' else x1, (y0 + y1) // 2)
elbow = (edge[0] - 35 if side == 'right' else edge[0] + 35, pt[1])
d.line([pt, elbow, edge], fill=line, width=4)
r = 7
d.ellipse((pt[0] - r, pt[1] - r, pt[0] + r, pt[1] + r), fill=(220, 45, 55), outline='white', width=2)
d.rectangle((0, 0, 1500, 57), fill=(5, 47, 87))
d.text((18, 13), 'Cranial cavity - superior view (anterior at top)', font=font, fill='white')
d.rectangle((965, 1180, 1499, 1279), fill=(245, 248, 251), outline=(150, 170, 190))
d.multiline_text((985, 1200), 'Labels identify gross features visible\nin this teaching model.\nRed lines are painted arteries.', font=small, fill=(45, 60, 72), spacing=5)
canvas.save(out, quality=95)
print(out)
python "/tmp/workspace/cranial-cavity-labelled/annotate.py" && python - <<'PY' from pathlib import Path p=Path('/tmp/workspace/cranial-cavity-labelled/output/labelled_cranial_cavity.jpg') print('exists=',p.exists(),'bytes=',p.stat().st_size if p.exists() else 0) PY
fc-match -f '%{file}\n' sans-serif | sed -n '1p'
~/cranial-cavity-labelled/annotate.py
Successfully replaced 1 instance(s) of the string in '/tmp/workspace/cranial-cavity-labelled/annotate.py'
python "/tmp/workspace/cranial-cavity-labelled/annotate.py" && python - <<'PY' from pathlib import Path p=Path('/tmp/workspace/cranial-cavity-labelled/output/labelled_cranial_cavity.jpg') print('exists=',p.exists(),'bytes=',p.stat().st_size if p.exists() else 0) PY
~/cranial-cavity-labelled/annotate.py
Successfully replaced 1 instance(s) of the string in '/tmp/workspace/cranial-cavity-labelled/annotate.py'
python "/tmp/workspace/cranial-cavity-labelled/annotate.py" && python - <<'PY' from pathlib import Path p=Path('/tmp/workspace/cranial-cavity-labelled/output/labelled_cranial_cavity.jpg') print('exists=', p.exists(), 'bytes=', p.stat().st_size) PY
labelled cranial cavity
Image · JPG