From b19f05859d062710f7ef08fd503869e7dd99d3b2 Mon Sep 17 00:00:00 2001 From: Tyler Kennedy Date: Sun, 1 Jan 2023 13:40:56 -0500 Subject: [PATCH] Apply image orientation metadata before resizing. (#345) --- core/files.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/files.py b/core/files.py index 21a862b..a7d34d8 100644 --- a/core/files.py +++ b/core/files.py @@ -24,6 +24,16 @@ def resize_image( to fit if needed) """ with Image.open(image) as img: + try: + # Take any orientation EXIF data, apply it, and strip the + # orientation data from the new image. + img = ImageOps.exif_transpose(img) + except Exception: # noqa + # exif_transpose can crash with different errors depending on + # the EXIF keys. Just ignore them all, better to have a rotated + # image than no image. + pass + if cover: resized_image = ImageOps.fit(img, size, method=Image.Resampling.BILINEAR) else: