Browse Source

Rotate thumbnails according to exif orientation

master
Marien Fressinaud 6 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

from operator import itemgetter from operator import itemgetter


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


from configuration import SITE_TITLE, THEME from configuration import SITE_TITLE, THEME






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

with Image.open(photo["path"]) as image: 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 w, h = image.size
if w > size[0] and h > size[1]: if w > size[0] and h > size[1]:
# If the original file is larger in width AND height, we resize # If the original file is larger in width AND height, we resize
image.save(output_path) image.save(output_path)




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


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

Loading…
Cancel
Save