import os
import shutil
import numpy as np
from PIL import Image
def resize_image(image_path, size, letterbox_image):
image = Image.open(image_path)
iw, ih = image.size
w, h = size
if letterbox_image:
scale = min(w / iw, h / ih)
nw = int(iw * scale)
nh = int(ih * scale)
image = image.resize((nw, nh), Image.BICUBIC)
new_image = Image.new('RGB', size, (128, 128, 128))
new_image.paste(image, ((w - nw) // 2, (h - nh) // 2))
new_image.save(image_path)
print("save success")
else:
new_image = image.resize((w, h), Image.BICUBIC)
new_image.save(image_path)
def is_dir(path):
if os.path.isdir(path):
for file_name in os.listdir(path):
is_dir(os.path.join(path, file_name))
else:
resize_image(path, (416,416), letterbox_image=True)
origin_images_folder = r"E:\myworld\my_like\using_torch\project1_classify\images\train"
save_folder = r"E:\myworld\my_like\using_torch\project1_classify\processed_images\train"
# 先复制文件夹的结构及内容,保存到save_folder中,然后在对save_folder中的文件进行修改,这样就不会影响原始数据
shutil.copytree(origin_images_folder, save_folder)
is_dir(save_folder)
批量resize图片并保存到另一个路径
最新推荐文章于 2024-08-23 08:00:52 发布