Skip to content

image

Strategy class for image/jpg.

ImageDataParseStrategy dataclass

Parse strategy for images.

Registers strategies:

  • ("mediaType", "image/jpg")
  • ("mediaType", "image/jpeg")
  • ("mediaType", "image/jp2")
  • ("mediaType", "image/png")
  • ("mediaType", "image/gif")
  • ("mediaType", "image/tiff")
  • ("mediaType", "image/eps")
Source code in oteapi/strategies/parse/image.py
@dataclass
class ImageDataParseStrategy:
    """Parse strategy for images.

    **Registers strategies**:

    - `("mediaType", "image/jpg")`
    - `("mediaType", "image/jpeg")`
    - `("mediaType", "image/jp2")`
    - `("mediaType", "image/png")`
    - `("mediaType", "image/gif")`
    - `("mediaType", "image/tiff")`
    - `("mediaType", "image/eps")`

    """

    parse_config: "ResourceConfig"

    def __post_init__(self):
        self.localpath = "/ote-data"
        self.filename = self.parse_config.configuration["filename"]
        self.conf = self.parse_config.configuration
        if "localpath" in self.conf:
            self.localpath = self.conf["localpath"]

    def initialize(self, session: "Optional[Dict[str, Any]]" = None) -> SessionUpdate:
        """Initialize."""
        return SessionUpdate()

    def get(
        self, session: "Optional[Dict[str, Any]]" = None
    ) -> SessionUpdateImageParse:
        if session is not None:
            self.conf.update(session)
        parsedOutput = {}
        if "crop" in self.conf:
            print("cropping!")
            im = Image.open(f"{self.localpath}/{self.filename}")
            crop = self.conf["crop"]
            im_cropped = im.crop(tuple(crop))
            cropped_filename = f"{self.localpath}/cropped_{self.filename}"
            im_cropped.save(cropped_filename)
            parsedOutput["cropped_filename"] = cropped_filename
        parsedOutput["parseImage"] = "Done"
        return SessionUpdateImageParse(parsedOutput=parsedOutput)

initialize(self, session=None)

Initialize.

Source code in oteapi/strategies/parse/image.py
def initialize(self, session: "Optional[Dict[str, Any]]" = None) -> SessionUpdate:
    """Initialize."""
    return SessionUpdate()

SessionUpdateImageParse (SessionUpdate) pydantic-model

Configuration model for ImageParse.

Source code in oteapi/strategies/parse/image.py
class SessionUpdateImageParse(SessionUpdate):
    """Configuration model for ImageParse."""

    parsedOutput: Dict[str, str] = Field(
        ..., description="Parsed output from ImageParse."
    )

parsedOutput: Dict[str, str] pydantic-field required

Parsed output from ImageParse.

Back to top