Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions simpletuner/helpers/image_manipulation/training_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import numpy as np
import trainingsample as tsr
from diffusers.utils.export_utils import export_to_gif
from PIL import Image
from PIL import ExifTags, Image
from PIL.ImageOps import exif_transpose
from tqdm import tqdm

Expand Down Expand Up @@ -709,7 +709,20 @@ def correct_image(self):
if self.image is not None and hasattr(self.image, "convert"):
# Convert image to RGB to remove any alpha channel and apply EXIF data transformations
self.image = self.image.convert("RGB")
self.image = exif_transpose(self.image)
try:
self.image = exif_transpose(self.image)
except TypeError as error:
# Pillow can read the orientation but fail to write an unrelated rational tag stored as text.
if str(error) != "bad operand type for abs(): 'str'":
raise
orientation = self.image.getexif().get(ExifTags.Base.Orientation)
if orientation not in range(2, 9):
raise
exif = Image.Exif()
exif[ExifTags.Base.Orientation] = orientation
self.image.info["exif"] = exif.tobytes()
self.image = exif_transpose(self.image)
logger.warning("Dropped malformed EXIF metadata while orienting %s.", self._image_path)
return self

def _apply_prepared_crop(self):
Expand Down
26 changes: 25 additions & 1 deletion tests/test_training_sample.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import os
import struct
import tempfile
import unittest
from unittest.mock import MagicMock, patch

import numpy as np
from PIL import Image
from PIL import ExifTags, Image, ImageOps

from simpletuner.helpers.data_backend.dataset_runtime import DatasetRuntime
from simpletuner.helpers.image_manipulation.batched_training_samples import BatchedTrainingSamples
Expand Down Expand Up @@ -125,6 +126,29 @@ def test_prepared_crop_uses_published_offsets_after_json_roundtrip(self):
np.testing.assert_array_equal(np.asarray(prepared.image), expected)
self.assertEqual(prepared.crop_coordinates, (11, 27))

def test_orientation_with_malformed_gps_latitude(self):
pixels = np.arange(18, dtype=np.uint8).reshape(2, 3, 3)
for orientation in range(2, 9):
with self.subTest(orientation=orientation):
image = Image.fromarray(pixels)
valid = Image.Exif()
valid[ExifTags.Base.Orientation] = orientation
image.info["exif"] = valid.tobytes()
expected = ImageOps.exif_transpose(image)

# Little-endian TIFF with a GPS subdirectory storing latitude as ASCII instead of rationals.
image.info["exif"] = b"Exif\x00\x00" + b"".join(
(
struct.pack("<2sHIH", b"II", 42, 8, 2),
struct.pack("<HHIHH", ExifTags.Base.Orientation, 3, 1, orientation, 0),
struct.pack("<HHIII", ExifTags.IFD.GPSInfo, 4, 1, 38, 0),
struct.pack("<HHHI4sI", 1, ExifTags.GPS.GPSLatitude, 2, 4, b"abc\x00", 0),
)
)
sample = TrainingSample(image, self.data_backend_id, {"original_size": image.size})
np.testing.assert_array_equal(np.asarray(sample.image), np.asarray(expected))
self.assertNotIn(ExifTags.Base.Orientation, sample.image.getexif())

def test_image_initialization(self):
"""Test that the image is correctly initialized and converted."""
sample = TrainingSample(self.image, self.data_backend_id, self.image_metadata)
Expand Down
Loading