返回提交历史
Modified
g4f/image/__init__.py
+15
-14
XFEstudio/gpt4free
Fix image orientation
20a47d85
代码差异
1 个文件
+15
-14
@@ -10,6 +10,7 @@ from typing import Optional
10
10
try:
11
11
from PIL.Image import open as open_image, new as new_image
12
12
from PIL.Image import FLIP_LEFT_RIGHT, ROTATE_180, ROTATE_270, ROTATE_90
13
from PIL import Image, ExifTags
13
14
has_requirements = True
14
15
except ImportError:
15
16
has_requirements = False
@@ -211,10 +212,10 @@ def get_orientation(image: Image) -> int:
211
212
int: The orientation value.
212
213
"""
213
214
exif_data = image.getexif() if hasattr(image, 'getexif') else image._getexif()
214
if exif_data is not None:
215
orientation = exif_data.get(274) # 274 corresponds to the orientation tag in EXIF
216
if orientation is not None:
217
return orientation
215
if exif_data:
216
for tag, value in ExifTags.TAGS.items():
217
if value == 'Orientation':
218
return exif_data.get(tag)
218
219
219
220
def process_image(image: Image, new_width: int = 800, new_height: int = 800) -> Image:
220
221
"""
@@ -229,16 +230,16 @@ def process_image(image: Image, new_width: int = 800, new_height: int = 800) ->
229
230
Image: The processed image.
230
231
"""
231
232
# Fix orientation
232
# orientation = get_orientation(image)
233
# if orientation:
234
# if orientation > 4:
235
# image = image.transpose(FLIP_LEFT_RIGHT)
236
# if orientation in [3, 4]:
237
# image = image.transpose(ROTATE_180)
238
# if orientation in [5, 6]:
239
# image = image.transpose(ROTATE_270)
240
# if orientation in [7, 8]:
241
# image = image.transpose(ROTATE_90)
233
orientation = get_orientation(image)
234
if orientation:
235
if orientation > 4:
236
image = image.transpose(FLIP_LEFT_RIGHT)
237
if orientation in [3, 4]:
238
image = image.transpose(ROTATE_180)
239
if orientation in [5, 6]:
240
image = image.transpose(ROTATE_270)
241
if orientation in [7, 8]:
242
image = image.transpose(ROTATE_90)
242
243
# Resize image
243
244
image.thumbnail((new_width, new_height))
244
245
# Remove transparency