Skip to main content

Image.Overlay

Examples below assume this module is imported with an imports: entry under alias Image. Kind references follow that alias — substitute your own if you import it under a different name.

Draws labelled rectangles onto an image via a server-side canvas (@napi-rs/canvas) and returns the annotated image in the requested format (PNG by default). Coordinates are pixels, top-left origin — the same space Pdf.Rasterizer reports.

Visualization, not mutation: shapes are drawn as given and clipped at the image edges — a box that hangs off the canvas renders partially, because showing a wrong proposal is the point of a review loop. Only non-finite coordinates or non-positive sizes are rejected.


Example

kind: Image.Overlay
metadata: { name: DrawBoxes }
stroke: { color: "#FF3B30", width: 3 }
label: { color: "#FFFFFF", placement: top-left }
- name: marked
inputs:
image: "${{ steps.page.result.image }}"
shapes:
- { x: 240, y: 64, width: 300, height: 40, label: "firstName (text)" }
- { x: 240, y: 128, width: 40, height: 40, color: "#0066FF" }
invoke: !ref DrawBoxes

Configuration

FieldTypeRequiredDescription
stroke.colorstringno (default #FF3B30)CSS color of the rectangle outline; a shape's color overrides it.
stroke.widthnumberno (default 3)Outline thickness in pixels.
label.colorstringno (default #FFFFFF)Label text color.
label.backgroundstringnoColor behind the label text; defaults to the shape's stroke color.
label.sizenumberno (default 14)Label font size in pixels.
label.placementtop-left | top-right | bottom-left | bottom-rightno (default top-left)Which corner of the rectangle the label tab anchors to — drawn outside the box and clamped into the image.
formatpng | jpeg | webpno (default png)Output image format. A per-invocation format takes precedence.
qualityintegerno (default 80)Encoder quality (1–100) for the lossy formats; ignored for png.

Invocation inputs

InputTypeRequiredDescription
imageUint8ArrayyesBuffered image bytes — PNG, JPEG, or WebP (anything the canvas decoder reads).
shapesarrayyesRectangles to draw; at least one.
formatpng | jpeg | webpnoOutput image format. Takes precedence over the resource-level format.
qualityintegernoEncoder quality (1–100) for the lossy formats; ignored for png.

Each shapes[] entry:

FieldTypeRequiredDescription
x, ynumberyesTop-left corner in pixels (top-left origin). May exceed the image bounds; the drawing is clipped.
width, heightnumberyesBox size in pixels; must be > 0.
labelstringnoTag drawn at the box's anchor corner.
colorstringnoPer-shape override of the stroke (and label background) color.

Output

FieldTypeDescription
imageUint8ArrayAnnotated image as buffered bytes in the chosen format.
widthintegerImage width in pixels (unchanged from the input).
heightintegerImage height in pixels (unchanged from the input).
mediaTypestringMIME type of the encoded image (image/png, image/jpeg, or image/webp).

Errors

CodeWhen
ERR_INVALID_INPUTimage is not a Uint8Array or not decodable as an image; a shape has non-finite coordinates or non-positive width/height; format is not png/jpeg/webp; or quality is not an integer between 1 and 100.