from PIL import Image import os def resize_images(input_folder, output_folder, width, height): # 确保输出文件夹存在 if not os.path.exists(output_folder): os.makedirs(output_folder) # 遍历输入文件夹中的所有文件 for filename in os.listdir(input_folder): if filename.endswith(('.png', '.jpg', '.jpeg', '.gif')): # 打开图片 with Image.open(os.path.join(input_folder, filename)) as img: # 调整尺寸 resized_img = img.resize((width, height), Image.LANCZOS) # 保存图片 resized_img.save(os.path.join(output_folder, filename)) # 设定输入文件夹、输出文件夹和目标尺寸 input_folder = "C:/Users/Dingchenjun/Desktop/图片" output_folder = "C:/Users/Dingchenjun/Desktop/图片2" target_width = 158 target_height = 177 # 调用函数 resize_images(input_folder, output_folder, target_width, target_height) //修改文件路径即可
批量裁剪文件尺寸
最新推荐文章于 2024-08-31 21:39:38 发布