Spaces:
Runtime error
Runtime error
File size: 1,718 Bytes
b89a93f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | import tempfile
import unittest
from pathlib import Path
from PIL import Image
from image_preprocessing import load_standardized_image
class LoadStandardizedImageTests(unittest.TestCase):
def test_applies_exif_orientation_and_returns_rgb_without_metadata(self):
with tempfile.TemporaryDirectory() as temp_dir:
image_path = Path(temp_dir) / "rotated.jpg"
image = Image.new("RGB", (4, 2), "red")
exif = image.getexif()
exif[274] = 6 # Rotate 90 degrees clockwise for display.
image.save(image_path, exif=exif)
standardized = load_standardized_image(image_path)
self.assertEqual(standardized.size, (2, 4))
self.assertEqual(standardized.mode, "RGB")
self.assertEqual(standardized.info, {})
self.assertNotIn(274, standardized.getexif())
def test_converts_non_rgb_images(self):
with tempfile.TemporaryDirectory() as temp_dir:
image_path = Path(temp_dir) / "grayscale.png"
Image.new("L", (3, 5), 128).save(image_path)
standardized = load_standardized_image(image_path)
self.assertEqual(standardized.size, (3, 5))
self.assertEqual(standardized.mode, "RGB")
def test_decodes_heic_images(self):
with tempfile.TemporaryDirectory() as temp_dir:
image_path = Path(temp_dir) / "iphone.heic"
Image.new("RGB", (6, 4), "blue").save(image_path, format="HEIF")
standardized = load_standardized_image(image_path)
self.assertEqual(standardized.size, (6, 4))
self.assertEqual(standardized.mode, "RGB")
if __name__ == "__main__":
unittest.main()
|