Skip to content

Commit 035a0e1

Browse files
committed
Fixed image loading
1 parent b17f98b commit 035a0e1

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

src/msfocr/llm/ocr_functions.py

+17-15
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,22 @@ 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
174-
try:
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
187-
return image.copy()
173+
image.load()
174+
175+
orientation = None
176+
try:
177+
for orientation in ExifTags.TAGS.keys():
178+
if ExifTags.TAGS[orientation] == 'Orientation':
179+
break
180+
exif = dict(image.getexif().items())
181+
if exif.get(orientation) == 3:
182+
image = image.rotate(180, expand=True)
183+
elif exif.get(orientation) == 6:
184+
image = image.rotate(270, expand=True)
185+
elif exif.get(orientation) == 8:
186+
image = image.rotate(90, expand=True)
187+
except (AttributeError, KeyError, IndexError):
188+
pass
189+
return image
188190

189191

0 commit comments

Comments
 (0)