Merge pull request #126 from qslug/fix-exif

Add error handling for bad exif, and rotate before sizing
This commit is contained in:
Victor Hall 2023-03-29 00:25:36 -04:00 committed by GitHub
commit b527f5b475
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 3 deletions

View File

@ -149,6 +149,14 @@ class ImageTrainItem:
self.error = None
self.__compute_target_width_height()
def load_image(self):
image = PIL.Image.open(self.pathname).convert('RGB')
try:
image = ImageOps.exif_transpose(image)
except SyntaxError as e:
pass
return image
def hydrate(self, crop=False, save=False, crop_jitter=20):
"""
crop: hard center crop to 512x512
@ -158,8 +166,7 @@ class ImageTrainItem:
# print(self.pathname, self.image)
try:
# if not hasattr(self, 'image'):
self.image = PIL.Image.open(self.pathname).convert('RGB')
self.image = ImageOps.exif_transpose(self.image)
self.image = self.load_image()
width, height = self.image.size
if crop:
@ -229,7 +236,7 @@ class ImageTrainItem:
def __compute_target_width_height(self):
self.target_wh = None
try:
with Image.open(self.pathname) as image:
with self.load_image() as image:
width, height = image.size
image_aspect = width / height
target_wh = min(self.aspects, key=lambda aspects:abs(aspects[0]/aspects[1] - image_aspect))