From 5cca9ad993fe0a68b7962f9e1b4450b811065a37 Mon Sep 17 00:00:00 2001 From: Fushan Wen Date: Wed, 5 Oct 2022 10:04:28 +0800 Subject: [PATCH] Replace `Image.LANCZOS` with `Image.Resampling.LANCZOS` Image.LANCZOS is deprecated since PIL 9.1.0 See also: https://pillow.readthedocs.io/en/stable/deprecations.html#constants --- wallpapers/generate_wallpaper_sizes.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/wallpapers/generate_wallpaper_sizes.py b/wallpapers/generate_wallpaper_sizes.py index 7b4c13de..b44acef8 100644 --- a/wallpapers/generate_wallpaper_sizes.py +++ b/wallpapers/generate_wallpaper_sizes.py @@ -3,8 +3,10 @@ import logging from pathlib import Path from itertools import chain +from typing import Final try: + import PIL from PIL import Image except ImportError: logging.critical("Please install the python PIL library.") @@ -26,6 +28,8 @@ templates = { 'vertical': ('vertical_base_size.png', 'vertical_base_size.jpg') } +PIL_VERSION: Final = tuple(map(int, PIL.__version__.split("."))) + for orientation in ('horizontal', 'vertical'): for file in chain(*map(Path().rglob, templates[orientation])): image = Image.open(file) @@ -41,6 +45,10 @@ for orientation in ('horizontal', 'vertical'): crop = int(base_width - width/(height/base_height))//2 box = (crop, 0, base_width-crop, base_height) else: box = None - resized_image = image.resize((width, height), Image.LANCZOS, box) + # Image.LANCZOS is deprecated since 9.1.0 https://pillow.readthedocs.io/en/stable/deprecations.html#constants + if PIL_VERSION >= (9, 1): + resized_image = image.resize((width, height), Image.Resampling.LANCZOS, box) + else: + resized_image = image.resize((width, height), Image.LANCZOS, box) resized_image.save(base_dir / f'{width}x{height}{extension}', quality=90, optimize=True, subsampling=1)