replace image.png thumbnail

This commit is contained in:
Karl Hudgell 2025-05-06 09:19:48 +01:00
parent 073cc3bda2
commit e43ab87922

View File

@ -6,6 +6,8 @@ def generate_thumbnail(image_path: str, size=(500, 500)) -> str:
Generates a thumbnail for a given image with a max size of 500x500, Generates a thumbnail for a given image with a max size of 500x500,
and saves it in a 'thumbnails' subdirectory alongside the original. and saves it in a 'thumbnails' subdirectory alongside the original.
If the filename includes the word "image", the thumbnail will always be overwritten.
Args: Args:
image_path (str): Path to the original image. image_path (str): Path to the original image.
size (tuple): Maximum width and height of the thumbnail. size (tuple): Maximum width and height of the thumbnail.
@ -20,7 +22,9 @@ def generate_thumbnail(image_path: str, size=(500, 500)) -> str:
filename = os.path.basename(image_path) filename = os.path.basename(image_path)
thumbnail_path = os.path.join(thumbnail_dir, filename) thumbnail_path = os.path.join(thumbnail_dir, filename)
if not os.path.exists(thumbnail_path): should_overwrite = "image" in filename.lower()
if not os.path.exists(thumbnail_path) or should_overwrite:
try: try:
img = Image.open(image_path) img = Image.open(image_path)
img.thumbnail(size, Image.Resampling.LANCZOS) img.thumbnail(size, Image.Resampling.LANCZOS)