Spaces:
Runtime error
Runtime error
Upload backend.py
Browse files- backend.py +21 -0
backend.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image, ExifTags
|
2 |
+
|
3 |
+
def check_image_rotation(image_path):
|
4 |
+
try:
|
5 |
+
image = Image.open(image_path)
|
6 |
+
for orientation in ExifTags.TAGS.keys():
|
7 |
+
if ExifTags.TAGS[orientation] == 'Orientation':
|
8 |
+
break
|
9 |
+
exif = image._getexif()
|
10 |
+
if exif is not None:
|
11 |
+
if orientation in exif:
|
12 |
+
if exif[orientation] == 3:
|
13 |
+
image = image.rotate(180, expand=True)
|
14 |
+
elif exif[orientation] == 6:
|
15 |
+
image = image.rotate(270, expand=True)
|
16 |
+
elif exif[orientation] == 8:
|
17 |
+
image = image.rotate(90, expand=True)
|
18 |
+
return image
|
19 |
+
except (AttributeError, KeyError, IndexError):
|
20 |
+
# If the orientation tag is not found or any other error occurs, return the original image
|
21 |
+
return Image.open(image_path)
|