废话少说,直接上代码,行不行试了就知道
import os
from PIL import Image
def letterbox_image(image, size):
iw, ih = image.size
w, h = size
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))
return new_image
size = (1920, 1080)
image_file = "img_voc_cat_dog"
image_list = os.listdir(image_file)
for img in image_list:
image_path = image_file + "/" + img
print(image_path)
image = Image.open(image_path)
print(image.size)
# image.show()
new_image = letterbox_image(image, size)
print(new_image.size)
# new_image.show()
save_path = 'save/' + img
new_image.save(save_path)
如果可行,别忘了点赞三连!
版权声明:本文为justwaityou1314原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。