From 0dff00061b699cff76857dc0b2f4e32806048c55 Mon Sep 17 00:00:00 2001 From: Arthur031221 Date: Fri, 2 Oct 2026 11:09:21 +0800 Subject: [PATCH] Apply EXIF orientation when loading pet images --- Code/deep_utils.py | 4 ++-- tests/test_pet_splits.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Code/deep_utils.py b/Code/deep_utils.py index dfefcbf..fd736a7 100644 --- a/Code/deep_utils.py +++ b/Code/deep_utils.py @@ -132,7 +132,7 @@ def validate_manifest(manifest): def pet_dataset(manifest, split, training=False, image_size=(64, 64)): - from PIL import Image + from PIL import Image, ImageOps records = [r for r in manifest["records"] if r["split"] == split] if not records: raise ValueError(f"Empty split: {split}") @@ -148,7 +148,7 @@ def pet_dataset(manifest, split, training=False, image_size=(64, 64)): def decode(index): record = records[int(index.numpy())] with Image.open(root / record["path"]) as image: - pixels = np.asarray(image.convert("RGB").resize( + pixels = np.asarray(ImageOps.exif_transpose(image).convert("RGB").resize( (width, height), resample=Image.Resampling.BILINEAR), dtype=np.float32) / 255.0 return pixels, np.float32(record["label"]) diff --git a/tests/test_pet_splits.py b/tests/test_pet_splits.py index b3c1283..7fed343 100644 --- a/tests/test_pet_splits.py +++ b/tests/test_pet_splits.py @@ -14,6 +14,25 @@ @unittest.skipUnless(importlib.util.find_spec("tensorflow"), "optional deep-learning dependencies") class PetSplits(unittest.TestCase): + def test_pet_dataset_applies_exif_orientation(self): + from deep_utils import pet_dataset + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + pixels = np.zeros((8, 12, 3), dtype=np.uint8) + pixels[:, :6] = [255, 0, 0] + pixels[:, 6:] = [0, 0, 255] + orientation = Image.Exif() + orientation[274] = 6 + Image.fromarray(pixels).save(root / "portrait.jpg", quality=100, + subsampling=0, exif=orientation) + manifest = {"root": str(root), "records": [ + {"path": "portrait.jpg", "label": 0, "split": "train"}]} + actual = next(iter(pet_dataset(manifest, "train", image_size=(12, 8))))[0][0].numpy() + self.assertGreater(actual[1, 1, 0], 0.9) # red at the top + self.assertLess(actual[1, 1, 2], 0.1) + self.assertGreater(actual[-2, 1, 2], 0.9) # blue at the bottom + self.assertLess(actual[-2, 1, 0], 0.1) + def test_reproducible_disjoint_splits_and_bad_image_reporting(self): from deep_utils import prepare_pets with tempfile.TemporaryDirectory() as temporary: