cellmap_flow.ai_annotate.images =============================== .. py:module:: cellmap_flow.ai_annotate.images .. autoapi-nested-parse:: 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 ---------- .. autoapisummary:: cellmap_flow.ai_annotate.images.MAX_PIXELS cellmap_flow.ai_annotate.images.ALLOWED_FORMATS Functions --------- .. autoapisummary:: cellmap_flow.ai_annotate.images.decode_model_image cellmap_flow.ai_annotate.images.png_base64 Module Contents --------------- .. py:data:: MAX_PIXELS :value: 16777216 .. py:data:: ALLOWED_FORMATS :value: ('PNG', 'JPEG', 'WEBP') .. py:function:: 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. .. py:function:: png_base64(image) ``image`` as base64 PNG text (no ``data:`` prefix), for a JSON response.