[docs]@app.route("/api/<data_source>/image/<name>")defcontextual_image(data_source:str,name:str):""" Get a contextual image. :param data_source: data source to get the image from :param name: the name of the image in `workspace.json` :return: the contextual image converted to png """path:str|None=get_contextual_image_path(data_source,name)ifpathisNone:returnf"Image {name} not found in source {data_source}",404LOG.info("Opening contextual image")image:Image|None=get_contextual_image(path)ifimageisNone:returnf"Failed to open image {name} from source {data_source}",500LOG.info("Converting contextual image")image_io=BytesIO()image.save(image_io,"png")image_io.seek(0)LOG.info("Serving converted contextual image")# Ensure that the converted images are cached by the clientresponse=send_file(image_io,mimetype='image/png')response.headers["Cache-Control"]="public, max-age=604800, immutable"returnresponse
[docs]@app.route("/api/<data_source>/image/<name>/size")defcontextual_image_size(data_source:str,name:str):""" Get the size of a contextual image. :param data_source: data source to get the image from :param name: the name of the image in `workspace.json` :return: the size of the contextual image """path:str|None=get_contextual_image_path(data_source,name)ifnotpath:returnf"Image {name} not found in source {data_source}",404size:tuple[int,int]|None=get_contextual_image_size(path)ifnotsize:returnf"Failed to get size of image {name} from source {data_source}",500return{"width":size[0],"height":size[1]}
[docs]@app.route("/api/<data_source>/image/<name>/recipe")defcontextual_image_recipe(data_source:str,name:str):""" Get the registering recipe of a contextual image. :param data_source: data source to get the image recipe from :param name: the name of the image in `workspace.json` :return: the registering recipe of the contextual image """path:str|None=get_contextual_image_recipe_path(data_source,name)ifnotpath:returnf"Could not find recipe for image {name} in source {data_source}",404# Get the recipe pointspoints:dict=load_points_dict(path)ifnotpoints:returnf"Could not find registering points at {path}",404returnpoints,200