Respect exif metadata when resizing photos

Closes #1663.
This commit is contained in:
Lonami Exo 2021-01-26 21:44:11 +01:00
parent 9a6bc5ae72
commit 320ab75818

View File

@ -53,6 +53,11 @@ def _resize_photo_if_needed(
# Don't use a `with` block for `image`, or `file` would be closed. # Don't use a `with` block for `image`, or `file` would be closed.
# See https://github.com/LonamiWebs/Telethon/issues/1121 for more. # See https://github.com/LonamiWebs/Telethon/issues/1121 for more.
image = PIL.Image.open(file) image = PIL.Image.open(file)
try:
kwargs = {'exif': image.info['exif']}
except KeyError:
kwargs = {}
if image.width <= width and image.height <= height: if image.width <= width and image.height <= height:
return file return file
@ -72,7 +77,7 @@ def _resize_photo_if_needed(
result.paste(image, mask=image.split()[alpha_index]) result.paste(image, mask=image.split()[alpha_index])
buffer = io.BytesIO() buffer = io.BytesIO()
result.save(buffer, 'JPEG') result.save(buffer, 'JPEG', **kwargs)
buffer.seek(0) buffer.seek(0)
return buffer return buffer