Commit 4d40e81f authored by Joel Collins's avatar Joel Collins
Browse files

Added API route to convert LST to PNG

parent 6175e649
Loading
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -29,10 +29,10 @@ from labthings import create_app
from labthings.extensions import find_extensions
from labthings.views import View

from openflexure_microscope.microscope import Microscope
from openflexure_microscope.api.utilities import init_default_extensions, list_routes
from openflexure_microscope.api.v2 import views
from openflexure_microscope.json import JSONEncoder
from openflexure_microscope.microscope import Microscope
from openflexure_microscope.paths import (
    OPENFLEXURE_EXTENSIONS_PATH,
    OPENFLEXURE_VAR_PATH,
@@ -135,6 +135,9 @@ labthing.add_root_link(views.ConfigurationProperty, "instrumentConfiguration")
# Attach stage resources
labthing.add_view(views.StageTypeProperty, "/instrument/stage/type")

# Attach camera resources
labthing.add_view(views.LSTImageProperty, "/instrument/camera/lst")

# Attach streams resources
labthing.add_view(views.MjpegStream, "/streams/mjpeg")
labthing.add_view(views.SnapshotStream, "/streams/snapshot")
+1 −0
Original line number Diff line number Diff line
from .actions import enabled_root_actions
from .camera import *
from .captures import *
from .instrument import *
from .stage import *
+47 −0
Original line number Diff line number Diff line
import logging
from io import BytesIO

import numpy as np
from flask import Response
from labthings import find_component
from labthings.views import PropertyView
from PIL import Image


class LSTImageProperty(PropertyView):
    """Gets the lens-shading table as an image"""

    responses = {
        200: {
            "content_type": "image/png",
            "description": "Lens-shading table in RGB format",
        }
    }

    def get(self):
        """
        Get the stage geometry.
        """
        microscope = find_component("org.openflexure.microscope")

        # Return intentionally empty response if no LST is available
        if microscope.get_configuration()["camera"]["type"] != "PiCameraStreamer":
            logging.warning("Requested an LST from a non-PiCameraStreamer camera")
            return ("", 204)
        # Return intentionally empty response if we have a valid PiCamera but no LST
        elif getattr(microscope.camera.camera, "lens_shading_table") is None:
            logging.warning("PiCamera.lens_shading_table returned as None")
            return ("", 204)
        else:
            lst = np.asarray(microscope.camera.camera.lens_shading_table)
            # R next to G1
            top = np.concatenate(lst[:2], axis=1)
            # G2 next to B
            bottom = np.concatenate(lst[2:], axis=1)
            # Combined into a 2x2 grid of greyscale images
            all_channels = np.concatenate((top, bottom), axis=0)
            # Create a PNG
            img_bytes = BytesIO()
            Image.fromarray(np.uint8(all_channels), "L").save(img_bytes, "PNG")
            # Return the image
            return Response(img_bytes.getvalue(), mimetype="image/png")