Add rotate thumbnail

This commit is contained in:
Sewon Ahn 2020-12-11 21:30:24 +09:00
parent d679bfaea9
commit 149890f2eb
2 changed files with 14 additions and 2 deletions

View file

@ -36,4 +36,4 @@ echo "---> 'cp test_data/index.html data/'"
cp test_data/index.html data/
echo "---> 'python distribusi/test.py -d ./data/'"
python distribusi/test.py -d ./data/
python distribusi/test.py -t -d ./data/

View file

@ -7,7 +7,7 @@ import subprocess
from io import BytesIO
import magic
from PIL import Image
from PIL import Image, ExifTags
import markdown
from distribusi.page_template import html_footer, html_head
@ -39,8 +39,20 @@ def thumbnail(image, name, args):
try:
size = (450, 450)
im = Image.open(image)
for orientation in ExifTags.TAGS.keys():
if ExifTags.TAGS[orientation] == 'Orientation':
break
exif = im._getexif()
im.thumbnail(size)
if exif[orientation] == 3:
im = im.rotate(180, expand=True)
elif exif[orientation] == 6:
im = im.rotate(270, expand=True)
elif exif[orientation] == 8:
im = im.rotate(90, expand=True)
if (im.mode == 'RGBA'):
bg = Image.new('RGBA', im.size, (255,255,255))
composite = Image.alpha_composite(bg, im)