数据预处理篇---image resize (不失真)

# -- coding: utf-8 --
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)
原图:(353*500)

数据预处理篇---image resize (不失真)_第1张图片

效果图:(1920*1080)

数据预处理篇---image resize (不失真)_第2张图片

你可能感兴趣的:(深度学习,python)