picture-annotation.py
Python script, ASCII text executable
1from pyscript import document 2from pyodide.ffi import create_proxy 3 4# Remove the loading message 5document.getElementById("python-loading").remove() 6 7document.getElementById("shape-options").style.display = "flex" 8 9image = document.getElementById("annotation-image") 10zone = document.getElementById("annotation-zone") 11 12 13shape_type = "" 14 15 16def switch_shape(event): 17global shape_type 18shape = event.currentTarget.id 19shape_type = shape 20print("Shape is now of type:", shape) 21 22 23def open_shape(event): 24print("Creating a new shape of type:", shape_type) 25new_shape = document.createElementNS("http://www.w3.org/2000/svg", "svg") 26new_shape.setAttribute("width", image.width) 27new_shape.setAttribute("height", image.height) 28 29if shape_type == "shape-bbox": 30rectangle = document.createElementNS("http://www.w3.org/2000/svg", "rect") 31rectangle.setAttribute("x", "10") 32rectangle.setAttribute("y", "10") 33rectangle.setAttribute("width", "100") 34rectangle.setAttribute("height", "100") 35rectangle.setAttribute("fill", "hotPink") 36new_shape.appendChild(rectangle) 37 38zone.appendChild(new_shape) 39 40 41for button in list(document.getElementById("shape-selector").children): 42button.addEventListener( 43"click", 44create_proxy(switch_shape) 45) 46print("Shape", button.id, "is available") 47 48 49 50zone.addEventListener( 51"click", 52create_proxy(open_shape) 53) 54