Add error handling for bad exif, and rotate before sizing

This commit is contained in:
Augusto de la Torre 2023-03-28 19:19:08 +02:00
parent 7df4606b26
commit dd98ebe080
1 changed files with 10 additions and 3 deletions

View File

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