From c249804db8974db51689d04274e01e66cd28817b Mon Sep 17 00:00:00 2001 From: Josh Humphries Date: Wed, 11 Dec 2024 20:42:31 +0000 Subject: [PATCH] feat: respect the exif orientation value stored with each image in the mss index --- iiif/profiles/mss.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/iiif/profiles/mss.py b/iiif/profiles/mss.py index 4b377e2..ca332d6 100644 --- a/iiif/profiles/mss.py +++ b/iiif/profiles/mss.py @@ -81,6 +81,15 @@ def __init__(self, source: 'MSSSourceFile', cause: Exception): super().__init__(f'Failed to convert source image', log=f'Failed to convert {source.file} ({source.emu_irn}) due to {cause}') +# a set of EXIF orientation values (both raw numbers and textual representations) which, +# if applied to an image, would cause the width and height to swap. +EXIF_ORIENTATION_SWAP_VALUES = { + "5", "Mirror horizontal and rotate 270 CW", + "6", "Rotate 90 CW", + "7", "Mirror horizontal and rotate 90 CW", + "8", "Rotate 270 CW", +} + class MSSImageInfo(ImageInfo): """ @@ -103,6 +112,13 @@ def __init__(self, profile_name: str, name: str, doc: dict): # a list of the EMu generated derivatives of the original file. The list should already be # in the ascending (width, height) order because the import process sorts it self.derivatives = doc.get('derivatives', []) + # exif orientation tag value + self.orientation = doc.get("orientation", None) + if self.orientation in EXIF_ORIENTATION_SWAP_VALUES: + # swap all widths and heights + self.width, self.height = self.height, self.width + for der in self.derivatives: + der["width"], der["height"] = der["height"], der["width"] def choose_file(self, target_size: Optional[Tuple[int, int]] = None) -> str: """