返回提交历史
Modified
g4f/image/__init__.py
+14
-27
XFEstudio/gpt4free
Update render_messages
be8366bb
代码差异
1 个文件
+14
-27
@@ -206,24 +206,6 @@ def extract_data_uri(data_uri: str) -> bytes:
206
206
data = base64.b64decode(data)
207
207
return data
208
208
209
def get_orientation_key() -> int:
210
for tag, value in ExifTags.TAGS.items():
211
if value == 'Orientation':
212
return tag
213
214
def get_orientation(image: Image) -> int:
215
"""
216
Gets the orientation of the given image.
217
218
Args:
219
image (Image): The image.
220
221
Returns:
222
int: The orientation value.
223
"""
224
exif_data = image.getexif() if hasattr(image, 'getexif') else image._getexif()
225
return exif_data.get(get_orientation_key()) if exif_data else None
226
227
209
def process_image(image: Image, new_width: int = 800, new_height: int = 400, save: str = None) -> Image:
228
210
"""
229
211
Processes the given image by adjusting its orientation and resizing it.
@@ -236,15 +218,20 @@ def process_image(image: Image, new_width: int = 800, new_height: int = 400, sav
236
218
Returns:
237
219
Image: The processed image.
238
220
"""
239
# Fix orientation
240
orientation = get_orientation(image)
241
if orientation:
242
debug.log(f"Image orientation: {orientation}")
243
if orientation == 6:
244
image = image.transpose(ROTATE_90)
245
elif orientation == 3:
246
image = image.transpose(ROTATE_180)
247
image.thumbnail((new_width, new_height))
221
for orientation in ExifTags.TAGS.keys():
222
if ExifTags.TAGS[orientation] == 'Orientation': break
223
if hasattr(image, 'getexif'):
224
exif_data = image.getexif()
225
else:
226
exif_data = image._getexif()
227
exif = dict(exif_data.items())
228
if exif[orientation] == 3 :
229
image = image.rotate(180, expand=True)
230
elif exif[orientation] == 6 :
231
image = image.rotate(270, expand=True)
232
elif exif[orientation] == 8 :
233
image = image.rotate(90, expand=True)
234
image.thumbnail((new_width, new_height), Image.ANTIALIAS)
248
235
# Remove transparency
249
236
if image.mode == "RGBA":
250
237
image.load()