Skip to content

Commit fb891ab

Browse files
committed
Correct image orientation based on EXIF data, if available
1 parent ebf88ae commit fb891ab

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

Diff for: src/trilium_py/utils/image_util.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from PIL import Image
21
from io import BytesIO
32

3+
from PIL import Image, ImageOps
4+
45

56
def compress_image_bytes(image_bytes, extension, quality=90):
67
"""
@@ -15,6 +16,10 @@ def compress_image_bytes(image_bytes, extension, quality=90):
1516
try:
1617
with BytesIO(image_bytes) as img_buffer:
1718
with Image.open(img_buffer) as img:
19+
# Correct image orientation based on EXIF data, if available
20+
# This ensures the image is properly oriented after conversion
21+
img = ImageOps.exif_transpose(img)
22+
1823
output_buffer = BytesIO()
1924
# PIL/pillow can only recognize JPEG, it does not know JPG...
2025
if extension == 'jpg':
@@ -40,3 +45,13 @@ def get_extension_from_image_mime(mime):
4045
return 'svg'
4146
else:
4247
return mime.split('/')[1]
48+
49+
50+
if __name__ == "__main__":
51+
image_file = '/home/nate/data/1/1.jpg'
52+
output_file = '/home/nate/data/1/1.webp'
53+
with open(image_file, 'rb') as f:
54+
image_bytes = f.read()
55+
compressed_image_bytes = compress_image_bytes(image_bytes, 'webp', 90)
56+
with open(output_file, 'wb') as f:
57+
f.write(compressed_image_bytes)

0 commit comments

Comments
 (0)