DeprecationWarning: ANTIALIAS is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.LANCZOS instead. downsampled_image = image.resize((target_width, target_height), Image.ANTIALIAS)
时间: 2024-04-14 11:28:32 浏览: 23
感谢提醒,根据 Pillow 库的更新,ANTIALIAS 在Pillow 10版本中将被移除。你可以使用 Resampling.LANCZOS 替代它进行图像的下采样。下面是更新后的示例代码:
```python
from PIL import Image, ImageResampling
import os
# 定义输入文件夹和输出文件夹路径
input_folder = "input_folder_path"
output_folder = "output_folder_path"
# 获取输入文件夹中的所有图像文件
image_files = [f for f in os.listdir(input_folder) if os.path.isfile(os.path.join(input_folder, f))]
# 循环处理每个图像文件
for file in image_files:
# 构建输入图像的完整路径
input_image_path = os.path.join(input_folder, file)
# 打开图像文件
image = Image.open(input_image_path)
# 定义目标分辨率
target_width = 800 # 目标宽度
target_height = 600 # 目标高度
# 调整图像大小
resized_image = image.resize((target_width, target_height), resample=ImageResampling.LANCZOS)
# 构建输出图像的完整路径
output_image_path = os.path.join(output_folder, file)
# 保存调整后的图像
resized_image.save(output_image_path)
```
在示例代码中,我们导入了新的模块 `ImageResampling` 和 `resample=ImageResampling.LANCZOS`,使用 `LANCZOS` 方法进行图像下采样。这样可以避免使用 `ANTIALIAS` 方法所产生的警告。
请确保在运行代码之前,已经安装了 Pillow 库,并提供正确的文件夹路径。