Skip to content
Commits on Source (2)
This diff is collapsed.
This diff is collapsed.
%% Cell type:code id: tags:
``` python
from lxml import etree as et
from pathlib import Path
from PIL import Image, ImageDraw
ns = {
'tei': 'http://www.tei-c.org/ns/1.0',
'xml': 'http://www.w3.org/XML/1998/namespace'
}
tei_path = Path('anno-event-2023_tei.xml')
```
%% Cell type:code id: tags:
``` python
root = et.parse(tei_path).getroot()
facs = root.find('.//tei:facsimile[@xml:id="facs_5"]', namespaces=ns)
root, facs
```
%% Cell type:code id: tags:
``` python
colors = {
'TextRegion': {
'fill': '#d9e1f2',
'outline': None
},
'Image': {
'fill': '#fce4d6',
'outline': None
},
'UnknownRegion': {
'fill': '#fff2cc',
'outline': None
},
'Line': {
'fill': '#f8cbad',
'outline': 'grey'
},
'Word': {
'fill': '#bdd7ee',
'outline': 'grey'
}
}
def draw_annotations(image_path, rends):
img = Image.open(image_path)
width, height = img.size
converted = img.convert("RGBA")
image_data = converted.tobytes()
img = Image.frombytes('RGBA', (width, height), image_data)
for facs in root.findall('.//tei:zone', namespaces=ns):
if facs.attrib['rendition'] in rends:
rect = ImageDraw.Draw(img)
shape = [
(int(facs.attrib['ulx']), int(facs.attrib['uly'])),
(int(facs.attrib['lrx']), int(facs.attrib['lry'])),
]
fill = colors[facs.attrib['rendition']]['fill']
outline = colors[facs.attrib['rendition']]['outline']
rect = rect.rectangle(shape, fill=fill, outline=outline)
img.save(image_path)
```
%% Cell type:code id: tags:
``` python
draw_annotations(Path('../5_region_segmentation.png'), ['TextRegion', 'Image', 'UnknownRegion'])
draw_annotations(Path('../6_line_detection.png'), ['Line'])
draw_annotations(Path('../7_word_detection.png'), ['Word'])
```
lxml==4.9.2
Pillow==10.1.0
\ No newline at end of file