AnnotatedImage[source]
An interactive image viewer that supports drawing, selecting, and styling geometric shapes (polygons, rectangles, points...) as annotations.
Under the hood, we use Konva to render the image and its annotations.
Examples
from pret import component, create_store, use_store_snapshot, use_event_callback
from pret.ui.metanno import AnnotatedImage
import time
# Reactive store holding the annotation list
img_state = create_store([
    {
        "id": "1",
        "type": "polygon",
        "points": [10, 10, 50, 20],
        "label": "OBJ",
        "style": "primary",
    }
])
# Style preset referenced from the annotation above
img_annotation_styles = {
    "primary": {
        "strokeColor": "red",
        "strokeWidth": 2,
        "fillColor": "rgba(255,0,0,0.5)",
        "labelPosition": "center",
        "align": "center",
        "verticalAlign": "top",
    },
    "secondary": {
        "strokeColor": "blue",
        "strokeWidth": 2,
        "fillColor": "#0000ff80",
        "textColor": "white",
    }
}
@component
def MyImage():
    tracked_state = use_store_snapshot(img_state)
    @use_event_callback
    def on_mouse_select(modkeys, shapes):
        # Toggle 'mouseSelected' flag
        for shp in shapes:
            img_state.append({
                "id": str(time.time()),
                "points": shp["points"],
                "label": "OBJ",
                "style": "primary",
            })
    @use_event_callback
    def on_click(shape_idx, modkeys):
        if shape_idx is None:
            img_state.clear()
        else:
            old_style = img_state[shape_idx].get("style", "primary")
            new_style = "secondary" if old_style == "primary" else "primary"
            img_state[shape_idx]["style"] = new_style
    return AnnotatedImage(
        image="https://picsum.photos/400/300",
        annotations=tracked_state,
        annotation_styles=img_annotation_styles,
        on_mouse_select=on_mouse_select,
        on_click=on_click,
    )
MyImage()
Parameters
| PARAMETER | DESCRIPTION | 
|---|---|
| annotations | List of annotation objects drawn on top of the image. Each annotation must contain the geometric  
   TYPE:  | 
| annotation_styles | Named style presets referenced by the  
   TYPE:  | 
| image | Source URL or base-64 data URI of the image to annotate.   TYPE:  | 
| mouse_selection | Temporary shapes being drawn by the user while the mouse button is held down. Supplied back to    TYPE:  | 
| style | Inline CSS-compatible style overrides for the root element of the component.   TYPE:  | 
| actions | Optional imperative handles (e.g.    TYPE:  | 
| key | React key for stable reconciliation.   TYPE:  | 
| on_click | Invoked when the user clicks on an existing shape. 
   TYPE:  | 
| on_key_press | Invoked when the component has focus and the user presses a key. 
   TYPE:  | 
| on_mouse_enter_shape | Hover callbacks fired when the mouse pointer enters or leaves a shape.   TYPE:  | 
| on_mouse_select | Fired after the user completes a drag selection. 
   TYPE:  |