Python程序
from PIL import Image import os def resize_images_in_folder(folder_path, output_folder, new_width, new_height): """ 更改指定文件夹中所有图片的分辨率。 :param folder_path: 包含原始图片的文件夹路径 :param output_folder: 存储更改分辨率后的图片的文件夹路径 :param new_width: 新图片的宽度 :param new_height: 新图片的高度 """ # 确保输出文件夹存在 if not os.path.exists(output_folder): os.makedirs(output_folder) # 遍历文件夹中的所有文件 for filename in os.listdir(folder_path): if filename.lower().endswith(('.png', '.jpg', '.JPG', '.jpeg', '.bmp', '.gif')): # 构建完整的文件路径 file_path = os.path.join(folder_path, filename) # 打开图片 with Image.open(file_path) as img: # 调整图片大小 img_resized = img.resize((new_width, new_height), Image.ANTIALIAS) # 构建输出文件的路径 output_path = os.path.join(output_folder, filename) # 保存更改分辨率后的图片 img_resized.save(output_path) print(f"图片已更改分辨率并保存:{output_path}") # 使用示例 folder_path = '' # 原始图片文件夹路径 output_folder = '' # 更改分辨率后图片的存储文件夹路径 new_width = 360 # 新宽度 new_height = 360 # 新高度 resize_images_in_folder(folder_path, output_folder, new_width, new_height)