{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from lxml import etree as et\n",
    "from pathlib import Path\n",
    "from PIL import Image, ImageDraw\n",
    "\n",
    "ns = {\n",
    "    'tei': 'http://www.tei-c.org/ns/1.0',\n",
    "    'xml': 'http://www.w3.org/XML/1998/namespace'\n",
    "}\n",
    "\n",
    "tei_path = Path('anno-event-2023_tei.xml')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "root = et.parse(tei_path).getroot()\n",
    "facs = root.find('.//tei:facsimile[@xml:id=\"facs_5\"]', namespaces=ns)\n",
    "\n",
    "root, facs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "colors = {\n",
    "    'TextRegion': {\n",
    "        'fill': '#d9e1f2',\n",
    "        'outline': None\n",
    "    },\n",
    "    'Image': {\n",
    "        'fill': '#fce4d6',\n",
    "        'outline': None\n",
    "    },\n",
    "    'UnknownRegion': {\n",
    "        'fill': '#fff2cc',\n",
    "        'outline': None\n",
    "    },\n",
    "    'Line': {\n",
    "        'fill': '#f8cbad',\n",
    "        'outline': 'grey'\n",
    "    },\n",
    "    'Word': {\n",
    "        'fill': '#bdd7ee',\n",
    "        'outline': 'grey'\n",
    "    }\n",
    "}\n",
    "\n",
    "def draw_annotations(image_path, rends):\n",
    "    img = Image.open(image_path)\n",
    "    width, height = img.size\n",
    "    converted = img.convert(\"RGBA\")\n",
    "    image_data = converted.tobytes()\n",
    "    img = Image.frombytes('RGBA', (width, height), image_data)\n",
    "    for facs in root.findall('.//tei:zone', namespaces=ns):\n",
    "        if facs.attrib['rendition'] in rends:\n",
    "            rect = ImageDraw.Draw(img)\n",
    "            shape = [\n",
    "                (int(facs.attrib['ulx']), int(facs.attrib['uly'])),\n",
    "                (int(facs.attrib['lrx']), int(facs.attrib['lry'])),\n",
    "            ]\n",
    "            fill = colors[facs.attrib['rendition']]['fill']\n",
    "            outline = colors[facs.attrib['rendition']]['outline']\n",
    "            rect = rect.rectangle(shape, fill=fill, outline=outline)\n",
    "    \n",
    "\n",
    "\n",
    "    img.save(image_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "draw_annotations(Path('../5_region_segmentation.png'), ['TextRegion', 'Image', 'UnknownRegion'])\n",
    "draw_annotations(Path('../6_line_detection.png'), ['Line'])\n",
    "draw_annotations(Path('../7_word_detection.png'), ['Word'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
