From 0eab26b8cc6ce74bc803e0b993e5f3e4b072db2a Mon Sep 17 00:00:00 2001
From: Julian Rother <julian@cccv.de>
Date: Sat, 27 May 2023 01:03:41 +0200
Subject: [PATCH] Fix thumbnail exif rotation

---
 warehouse/models.py | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/warehouse/models.py b/warehouse/models.py
index f96c8c8..67102e7 100644
--- a/warehouse/models.py
+++ b/warehouse/models.py
@@ -6,6 +6,7 @@ import datetime
 from flask import current_app, send_file, Markup
 from flask_sqlalchemy import SQLAlchemy
 from PIL import Image
+from PIL.ImageOps import exif_transpose
 
 db = SQLAlchemy()
 
@@ -48,11 +49,12 @@ class Photo(db.Model):
 	def send_thumbnail(self, max_width, max_height):
 		path = os.path.join(current_app.config['UPLOAD_FOLDER'], self.path)
 		buf = io.BytesIO()
-		with Image.open(path, formats=['JPEG']) as img:
-			scale = min(max_width/img.width, max_height/img.height)
-			img.thumbnail((int(img.width*scale), int(img.height*scale)))
-			img.save(buf, format='JPEG', quality=70)
-			buf.seek(0)
+		with Image.open(path, formats=['JPEG']) as img_orig:
+			with exif_transpose(img_orig) as img:
+				scale = min(max_width/img.width, max_height/img.height)
+				img.thumbnail((int(img.width*scale), int(img.height*scale)))
+				img.save(buf, format='JPEG', quality=70)
+				buf.seek(0)
 		return send_file(buf, mimetype='image/jpeg')
 
 def token_typable():
-- 
GitLab