def get_image()

in plugins/mkdocs-atlas-formatting-plugin/mkdocs_atlas_formatting_plugin/atlaswebserver.py [0:0]


    def get_image(self, uri: str) -> Tuple[str, int, int]:
        """
        Given an Atlas URI, fetch a PNG image from the running Atlas Standalone server. Encode
        the image as a Base64 string suitable for embedding as a data uri in an image tag.

        :param uri: Atlas URI
        :return: image data uri, image width, image height
        """
        url = self.base_url + uri

        r = requests.get(url)

        if not r.ok:
            logger.error(f'failed to get image: code={r.status_code} text={r.text} url={url}')
            return '<pre>ERROR: failed to get image</pre>', 0, 0

        content_type = r.headers['Content-Type']
        encoded = str(b64encode(r.content), 'utf-8')
        data_uri = f'data:{content_type};base64,{encoded}'

        with BytesIO(r.content) as buffer:
            with Image.open(buffer) as image:
                width, height = image.size

        return data_uri, width, height