Ported from os to pathlib

This commit is contained in:
mandlm 2018-11-07 23:43:29 +01:00
parent 71b1b29c3f
commit e4de3b3148

View File

@ -1,7 +1,6 @@
#!/usr/bin/python3 #!/usr/bin/python3
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 os import listdir, makedirs
from random import choice from random import choice
from pathlib import Path from pathlib import Path
from PIL import Image from PIL import Image
@ -18,15 +17,17 @@ def random():
@app.route("/random_image/") @app.route("/random_image/")
def random_image(): def random_image():
filename = choice(listdir(config.imgdir)) imgdir = Path(config.imgdir)
return redirect(url_for("image", filename=filename)) images = list(imgdir.glob("*.jpg"))
selected_image = choice(images).relative_to(imgdir)
return redirect(url_for("image", filename=selected_image))
@app.route("/img/<path:filename>") @app.route("/img/<path:filename>")
def image(filename): def image(filename):
scaled_img_dir = Path(config.imgdir) / ".slider" / "fhd" scaled_img_dir = Path(config.imgdir) / ".slider" / "fhd"
if not scaled_img_dir.exists(): if not scaled_img_dir.exists():
makedirs(scaled_img_dir) scaled_img_dir.mkdir(parents=True)
if not (scaled_img_dir / filename).exists(): if not (scaled_img_dir / filename).exists():
img = Image.open(Path(config.imgdir) / filename) img = Image.open(Path(config.imgdir) / filename)
img.thumbnail((1920, 1080)) img.thumbnail((1920, 1080))