From e4de3b31488d6f42a1065d99282cfcb678b6ab98 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Wed, 7 Nov 2018 23:43:29 +0100 Subject: [PATCH] Ported from os to pathlib --- slider.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/slider.py b/slider.py index d17fa05..56d13f7 100755 --- a/slider.py +++ b/slider.py @@ -1,7 +1,6 @@ #!/usr/bin/python3 from flask import Flask, render_template, send_from_directory, redirect, url_for -from os import listdir, makedirs from random import choice from pathlib import Path from PIL import Image @@ -18,15 +17,17 @@ def random(): @app.route("/random_image/") def random_image(): - filename = choice(listdir(config.imgdir)) - return redirect(url_for("image", filename=filename)) + imgdir = Path(config.imgdir) + images = list(imgdir.glob("*.jpg")) + selected_image = choice(images).relative_to(imgdir) + return redirect(url_for("image", filename=selected_image)) @app.route("/img/") def image(filename): scaled_img_dir = Path(config.imgdir) / ".slider" / "fhd" if not scaled_img_dir.exists(): - makedirs(scaled_img_dir) + scaled_img_dir.mkdir(parents=True) if not (scaled_img_dir / filename).exists(): img = Image.open(Path(config.imgdir) / filename) img.thumbnail((1920, 1080))