cellmap_flow.ai_annotate.images

Checking the images a model sends back, and encoding images for the browser.

A model’s response is untrusted input: its image could be huge, malformed or in a format whose decoder has a history of bugs. decode_model_image reads only the header first (PIL’s open is lazy), checks the format and size from it, and decodes the pixels only when both pass. Nothing the model returned goes to the browser as is: png_base64 re-encodes the decoded pixels as a fresh PNG.

Attributes

MAX_PIXELS

ALLOWED_FORMATS

Functions

decode_model_image(data)

The model's image as RGB, after checking its format and size before decoding.

png_base64(image)

image as base64 PNG text (no data: prefix), for a JSON response.

Module Contents

cellmap_flow.ai_annotate.images.MAX_PIXELS = 16777216
cellmap_flow.ai_annotate.images.ALLOWED_FORMATS = ('PNG', 'JPEG', 'WEBP')
cellmap_flow.ai_annotate.images.decode_model_image(data)

The model’s image as RGB, after checking its format and size before decoding.

Raises AIAnnotateError("bad_response") for anything other than a PNG, JPEG or WebP of at most MAX_PIXELS pixels, or one that fails to decode.

cellmap_flow.ai_annotate.images.png_base64(image)

image as base64 PNG text (no data: prefix), for a JSON response.