import os
from PIL import Image
def resize_images_in_folder(input_folder, output_folder, target_width=854, target_height=480):
# 确保输出文件夹存在,如果不存在则创建它
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# 遍历输入文件夹中的所有文件
for filename in os.listdir(input_folder):
input_file_path = os.path.join(input_folder, filename)
output_file_path = os.path.join(output_folder, filename)
# 如果是PNG文件,则调整大小
if filename.endswith(".png"):
resize_image(input_file_path, output_file_path, target_width, target_height)
print(f"调整大小: {input_file_path} -> {output_file_path}")
def resize_image(input_image_path, output_image_path, target_width=854, target_height=480):
try:
# 打开图像文件
image = Image.open(input_image_path)
# 调整图像大小
resized_image = image.resize((target_width, target_height))
# 保存调整大小后的图像
resized_image.save(output_image_path)
except Exception as e:
print(f"发生错误: {e}")
# 输入文件夹路径和输出文件夹路径
input_folder = "E:\SD\my01\my1\img2img_key" # 输入文件夹路径
output_folder = "E:\SD\my01\my1\img" # 输出文件夹路径
# 调整文件夹中的图像大小
resize_images_in_folder(input_folder, output_folder)
标签:target,img,output,image,input,path,folder
From: https://www.cnblogs.com/idazhi/p/18107248