use exif tags for image rotation

This commit is contained in:
j 2024-08-23 18:33:54 +02:00
parent f245824448
commit 1ccc8df75c
2 changed files with 14 additions and 9 deletions

View file

@ -18,7 +18,7 @@ import ox
import ox.image import ox.image
from ox.utils import json from ox.utils import json
from django.conf import settings from django.conf import settings
from PIL import Image from PIL import Image, ImageOps
from .chop import Chop, make_keyframe_index from .chop import Chop, make_keyframe_index
@ -441,10 +441,15 @@ def frame_direct(video, target, position):
r = run_command(cmd) r = run_command(cmd)
return r == 0 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): def resize_image(image_source, image_output, width=None, size=None):
if exists(image_source): if exists(image_source):
source = Image.open(image_source).convert('RGB') source = open_image_rgb(image_source)
source_width = source.size[0] source_width = source.size[0]
source_height = source.size[1] source_height = source.size[1]
if size: if size:

View file

@ -22,7 +22,7 @@ from oxdjango.sortmodel import get_sort_field
from person.models import get_name_sort from person.models import get_name_sort
from item.models import Item from item.models import Item
from annotation.models import Annotation 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 archive.chunk import save_chunk
from user.models import Group from user.models import Group
from user.utils import update_groups from user.utils import update_groups
@ -552,10 +552,10 @@ class Document(models.Model, FulltextMixin):
if len(crop) == 4: if len(crop) == 4:
path = os.path.join(folder, '%dp%d,%s.jpg' % (1024, page, ','.join(map(str, crop)))) path = os.path.join(folder, '%dp%d,%s.jpg' % (1024, page, ','.join(map(str, crop))))
if not os.path.exists(path): if not os.path.exists(path):
img = Image.open(src).crop(crop) img = open_image_rgb(src).crop(crop)
img.save(path) img.save(path)
else: else:
img = Image.open(path) img = open_image_rgb(path)
src = path src = path
if size < max(img.size): if size < max(img.size):
path = os.path.join(folder, '%dp%d,%s.jpg' % (size, page, ','.join(map(str, crop)))) 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: if len(crop) == 4:
path = os.path.join(folder, '%s.jpg' % ','.join(map(str, crop))) path = os.path.join(folder, '%s.jpg' % ','.join(map(str, crop)))
if not os.path.exists(path): 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) img.save(path)
else: else:
img = Image.open(path) img = open_image_rgb(path)
src = path src = path
if size < max(img.size): if size < max(img.size):
path = os.path.join(folder, '%sp%s.jpg' % (size, ','.join(map(str, crop)))) 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): if os.path.exists(src) and not os.path.exists(path):
image_size = max(self.width, self.height) image_size = max(self.width, self.height)
if image_size == -1: if image_size == -1:
image_size = max(*Image.open(src).size) image_size = max(*open_image_rgb(src).size)
if size > image_size: if size > image_size:
path = src path = src
else: else:
@ -606,7 +606,7 @@ class Document(models.Model, FulltextMixin):
self.pages = utils.pdfpages(self.file.path) self.pages = utils.pdfpages(self.file.path)
elif self.width == -1: elif self.width == -1:
self.pages = -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): def get_ratio(self):
if self.extension == 'pdf': if self.extension == 'pdf':