From 1ccc8df75c2a5097be2064a4ceb191743f3b2e6e Mon Sep 17 00:00:00 2001 From: j Date: Fri, 23 Aug 2024 18:33:54 +0200 Subject: [PATCH] use exif tags for image rotation --- pandora/archive/extract.py | 9 +++++++-- pandora/document/models.py | 14 +++++++------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/pandora/archive/extract.py b/pandora/archive/extract.py index 1cd54f558..3d6c74fe6 100644 --- a/pandora/archive/extract.py +++ b/pandora/archive/extract.py @@ -18,7 +18,7 @@ import ox import ox.image from ox.utils import json from django.conf import settings -from PIL import Image +from PIL import Image, ImageOps from .chop import Chop, make_keyframe_index @@ -441,10 +441,15 @@ def frame_direct(video, target, position): r = run_command(cmd) return r == 0 +def open_image_rgb(image_source): + source = Image.open(image_source) + source = ImageOps.exif_transpose(source) + source = source.convert('RGB') + return source def resize_image(image_source, image_output, width=None, size=None): if exists(image_source): - source = Image.open(image_source).convert('RGB') + source = open_image_rgb(image_source) source_width = source.size[0] source_height = source.size[1] if size: diff --git a/pandora/document/models.py b/pandora/document/models.py index d60bd27bb..68819f392 100644 --- a/pandora/document/models.py +++ b/pandora/document/models.py @@ -22,7 +22,7 @@ from oxdjango.sortmodel import get_sort_field from person.models import get_name_sort from item.models import Item from annotation.models import Annotation -from archive.extract import resize_image +from archive.extract import resize_image, open_image_rgb from archive.chunk import save_chunk from user.models import Group from user.utils import update_groups @@ -552,10 +552,10 @@ class Document(models.Model, FulltextMixin): if len(crop) == 4: path = os.path.join(folder, '%dp%d,%s.jpg' % (1024, page, ','.join(map(str, crop)))) if not os.path.exists(path): - img = Image.open(src).crop(crop) + img = open_image_rgb(src).crop(crop) img.save(path) else: - img = Image.open(path) + img = open_image_rgb(path) src = path if size < max(img.size): path = os.path.join(folder, '%dp%d,%s.jpg' % (size, page, ','.join(map(str, crop)))) @@ -568,10 +568,10 @@ class Document(models.Model, FulltextMixin): if len(crop) == 4: path = os.path.join(folder, '%s.jpg' % ','.join(map(str, crop))) if not os.path.exists(path): - img = Image.open(src).convert('RGB').crop(crop) + img = open_image_rgb(src).convert('RGB').crop(crop) img.save(path) else: - img = Image.open(path) + img = open_image_rgb(path) src = path if size < max(img.size): path = os.path.join(folder, '%sp%s.jpg' % (size, ','.join(map(str, crop)))) @@ -580,7 +580,7 @@ class Document(models.Model, FulltextMixin): if os.path.exists(src) and not os.path.exists(path): image_size = max(self.width, self.height) if image_size == -1: - image_size = max(*Image.open(src).size) + image_size = max(*open_image_rgb(src).size) if size > image_size: path = src else: @@ -606,7 +606,7 @@ class Document(models.Model, FulltextMixin): self.pages = utils.pdfpages(self.file.path) elif self.width == -1: self.pages = -1 - self.width, self.height = Image.open(self.file.path).size + self.width, self.height = open_image_rgb(self.file.path).size def get_ratio(self): if self.extension == 'pdf':