import os
from PIL import Imagedef resize_images(folder_path):
# 确保文件夹存在
if not os.path.exists(folder_path):
print(f"文件夹 {folder_path} 不存在")
return# 遍历文件夹中的所有文件
for filename in os.listdir(folder_path):
if filename.endswith(".png"):
img_path = os.path.join(folder_path, filename)
with Image.open(img_path) as img:
# 检查图像尺寸
if img.size == size_ori:
# 调整图像尺寸
resized_img = img.resize(size_goto, Image.LANCZOS)
resized_img.save(img_path)
print(f"{filename} 已成功调整为 1024x1280 像素")
else:
print(f"{filename} 尺寸不符合要求,跳过")# 使用示例
folder_path = "/your/images/path"size_ori = (360, 288)
size_goto = (1280, 1024)
resize_images(folder_path)
使用方法:将folder_path修改为你的存放图像的路径
size_ori改为你的原始图像尺寸
size_goto改为你希望得到的尺寸即可