Skip to content

Commit b17f98b

Browse files
committed
fix the correct_image_orientation function
1 parent 5f1155b commit b17f98b

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/msfocr/llm/ocr_functions.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -170,22 +170,20 @@ def correct_image_orientation(image_path):
170170
:return: PIL.Image.Image: The image with corrected orientation.
171171
"""
172172
with Image.open(image_path) as image:
173+
orientation = None
173174
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
189187
return image.copy()
190188

191189

0 commit comments

Comments
 (0)