Pre-scale and cache images to full-hd resolution

This commit is contained in:
Michael Mandl 2018-11-06 20:13:35 +01:00
parent ad4be9baf3
commit 8a3edce278

View file

@ -2,8 +2,10 @@
from flask import Flask, render_template, send_from_directory, redirect, url_for from flask import Flask, render_template, send_from_directory, redirect, url_for
from random import randint from random import randint
from os import listdir from os import listdir, makedirs
from random import choice from random import choice
from pathlib import Path
from PIL import Image
import config import config
@ -23,7 +25,15 @@ def random_image():
@app.route("/img/<path:filename>") @app.route("/img/<path:filename>")
def image(filename): def image(filename):
return send_from_directory(config.imgdir, filename) scaled_img_dir = Path(config.imgdir) / ".slider" / "fhd"
if not scaled_img_dir.exists():
makedirs(scaled_img_dir)
if not (scaled_img_dir / filename).exists():
img = Image.open(Path(config.imgdir) / filename)
img.thumbnail((1920, 1080))
img.save(scaled_img_dir / filename)
return send_from_directory(scaled_img_dir, filename)
if __name__ == "__main__": if __name__ == "__main__":