|
|
@@ -3,7 +3,6 @@ |
|
|
|
import os |
|
|
|
import shutil |
|
|
|
import re |
|
|
|
import subprocess |
|
|
|
import yaml |
|
|
|
|
|
|
|
from operator import attrgetter, itemgetter |
|
|
@@ -15,8 +14,6 @@ from PIL import Image |
|
|
|
from configuration import OUTPUT_DIR, PICTURES_DIR, SIZES, THEME_DIR |
|
|
|
|
|
|
|
|
|
|
|
FNULL = open(os.devnull, "w") |
|
|
|
|
|
|
|
jinja_env = Environment( |
|
|
|
loader=PackageLoader("revelateur", THEME_DIR), |
|
|
|
autoescape=select_autoescape(["html"]), |
|
|
@@ -169,8 +166,8 @@ def generate_webp_photo_for_size( |
|
|
|
if output_path.exists(): |
|
|
|
return |
|
|
|
|
|
|
|
command = ["cwebp", "-q", "80", jpg_output_path, "-o", output_path] |
|
|
|
subprocess.Popen(command, stdout=FNULL, stderr=subprocess.STDOUT).communicate() |
|
|
|
image = Image.open(jpg_output_path) |
|
|
|
image.save(output_path, format="webp", icc_profile=image.info.get("icc_profile")) |
|
|
|
|
|
|
|
|
|
|
|
def generate_jpg_photo_for_size(gallery_output_path, photo, width, height): |
|
|
@@ -179,27 +176,9 @@ def generate_jpg_photo_for_size(gallery_output_path, photo, width, height): |
|
|
|
if output_path.exists(): |
|
|
|
return output_path |
|
|
|
|
|
|
|
with Image.open(photo["path"]) as image: |
|
|
|
w, h = image.size |
|
|
|
if w > width and h > height: |
|
|
|
# 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. in case of (440, 264): |
|
|
|
# 1200x900 is resized to 440x330 |
|
|
|
# 1200x600 is resized to 528x264 |
|
|
|
if width / height <= w / h: |
|
|
|
w = int(max(height * w / h, 1)) |
|
|
|
h = height |
|
|
|
else: |
|
|
|
h = int(max(width * h / w, 1)) |
|
|
|
w = width |
|
|
|
new_size = (w, h) |
|
|
|
|
|
|
|
image.draft(None, new_size) |
|
|
|
image = image.resize(new_size, Image.BICUBIC) |
|
|
|
|
|
|
|
image.save(output_path) |
|
|
|
image = Image.open(photo["path"]) |
|
|
|
image.thumbnail((width, height), resample=Image.LANCZOS) |
|
|
|
image.save(output_path, icc_profile=image.info.get("icc_profile")) |
|
|
|
|
|
|
|
return output_path |
|
|
|
|