Browse Source

Rotate thumbnails according to exif orientation

master
Marien Fressinaud 5 years ago
parent
commit
9f79ac185b
1 changed files with 19 additions and 2 deletions
  1. 19
    2
      boop.py

+ 19
- 2
boop.py View File

@@ -7,7 +7,7 @@ import re
from operator import itemgetter

from jinja2 import Environment, PackageLoader, select_autoescape
from PIL import Image
from PIL import Image, ExifTags

from configuration import SITE_TITLE, THEME

@@ -116,8 +116,19 @@ def generate_gallery_dir(output_path, gallery):


def generate_thumb_file(output_path, photo):
orientation_key = get_orientation_exif_key()
size = (440, 264)

with Image.open(photo["path"]) as image:
size = (440, 264)
# First, make sure image is correctly oriented
exif = image._getexif()
if exif[orientation_key] == 3:
image = image.rotate(180, expand=True)
elif exif[orientation_key] == 6:
image = image.rotate(270, expand=True)
elif exif[orientation_key] == 8:
image = image.rotate(90, expand=True)

w, h = image.size
if w > size[0] and h > size[1]:
# If the original file is larger in width AND height, we resize
@@ -155,6 +166,12 @@ def generate_thumb_file(output_path, photo):
image.save(output_path)


def get_orientation_exif_key():
for (key, tag) in ExifTags.TAGS.items():
if tag == "Orientation":
return key


def main():
pictures_folder = os.path.join(os.curdir, PICTURES_DIR_NAME)
galleries = list(list_galleries_in(pictures_folder))

Loading…
Cancel
Save