|
|
|
|
|
|
|
|
import os |
|
|
import os |
|
|
import shutil |
|
|
import shutil |
|
|
import re |
|
|
import re |
|
|
import subprocess |
|
|
|
|
|
import yaml |
|
|
import yaml |
|
|
|
|
|
|
|
|
from operator import attrgetter, itemgetter |
|
|
from operator import attrgetter, itemgetter |
|
|
|
|
|
|
|
|
from configuration import OUTPUT_DIR, PICTURES_DIR, SIZES, THEME_DIR |
|
|
from configuration import OUTPUT_DIR, PICTURES_DIR, SIZES, THEME_DIR |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FNULL = open(os.devnull, "w") |
|
|
|
|
|
|
|
|
|
|
|
jinja_env = Environment( |
|
|
jinja_env = Environment( |
|
|
loader=PackageLoader("revelateur", THEME_DIR), |
|
|
loader=PackageLoader("revelateur", THEME_DIR), |
|
|
autoescape=select_autoescape(["html"]), |
|
|
autoescape=select_autoescape(["html"]), |
|
|
|
|
|
|
|
|
if output_path.exists(): |
|
|
if output_path.exists(): |
|
|
return |
|
|
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): |
|
|
def generate_jpg_photo_for_size(gallery_output_path, photo, width, height): |
|
|
|
|
|
|
|
|
if output_path.exists(): |
|
|
if output_path.exists(): |
|
|
return output_path |
|
|
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 |
|
|
return output_path |
|
|
|
|
|
|