@@ -170,22 +170,20 @@ def correct_image_orientation(image_path):
170
170
:return: PIL.Image.Image: The image with corrected orientation.
171
171
"""
172
172
with Image .open (image_path ) as image :
173
+ orientation = None
173
174
try :
174
- Image .open (image_path )
175
- exif = image ._getexif ()
176
- if exif is not None :
177
- orientation_key = next (
178
- key for key , value in ExifTags .TAGS .items () if value == 'Orientation'
179
- )
180
- orientation = exif .get (orientation_key )
181
- if orientation == 3 :
182
- image = image .rotate (180 , expand = True )
183
- elif orientation == 6 :
184
- image = image .rotate (270 , expand = True )
185
- elif orientation == 8 :
186
- image = image .rotate (90 , expand = True )
187
- except (AttributeError , KeyError , IndexError , StopIteration ) as e :
188
- print (f"Error correcting image orientation: { e } " )
175
+ for orientation in ExifTags .TAGS .keys ():
176
+ if ExifTags .TAGS [orientation ] == 'Orientation' :
177
+ break
178
+ exif = dict (image .getexif ().items ())
179
+ if exif .get (orientation ) == 3 :
180
+ image = image .rotate (180 , expand = True )
181
+ elif exif .get (orientation ) == 6 :
182
+ image = image .rotate (270 , expand = True )
183
+ elif exif .get (orientation ) == 8 :
184
+ image = image .rotate (90 , expand = True )
185
+ except (AttributeError , KeyError , IndexError ):
186
+ pass
189
187
return image .copy ()
190
188
191
189
0 commit comments