Browse Source

Generate real thumbnails

master
Marien Fressinaud 5 years ago
parent
commit
58bbeb6dfc
2 changed files with 43 additions and 2 deletions
  1. 42
    1
      boop.py
  2. 1
    1
      requirements.txt

+ 42
- 1
boop.py View File

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

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

from configuration import SITE_TITLE, THEME

@@ -110,7 +111,47 @@ def generate_gallery_dir(output_path, gallery):
shutil.copyfile(photo["path"], photo_output_path)

thumb_output_path = os.path.join(output_path, photo["thumb_url"])
shutil.copyfile(photo["path"], thumb_output_path)
generate_thumb_file(thumb_output_path, photo)


def generate_thumb_file(output_path, photo):
with Image.open(photo["path"]) as image:
size = (440, 264)
w, h = image.size
if w > size[0] and h > size[1]:
# If the original file is larger in width AND height, we resize
# first the image to the lowest size accepted (both width and
# height stays greater or equal to requested size).
# E.g. 1200x900 is resized to 440x330
# 1200x600 is resized to 528x264
if size[0] / size[1] <= w / h:
w = int(max(size[1] * w / h, 1))
h = 264
else:
h = int(max(size[0] * h / w, 1))
w = 440
new_size = (w, h)

image.draft(None, new_size)
image = image.resize(new_size, Image.BICUBIC)

# We now have an image with at least w = 440 OR h = 264 (unless one of
# the size is smaller). But the image can still be larger than
# requested size, so we have to crop the image in the middle.
crop_box = None
if w > size[0]:
left = (w - size[0]) / 2
right = left + size[0]
crop_box = (left, 0, right, h)
elif h > size[1]:
upper = (h - size[1]) / 2
lower = upper + size[1]
crop_box = (0, upper, w, lower)
if crop_box is not None:
image = image.crop(crop_box)

# And we save the final image.
image.save(output_path)


def main():

+ 1
- 1
requirements.txt View File

@@ -1,2 +1,2 @@
black
Jinja2
Pillow

Loading…
Cancel
Save