RBG 加入灰条
def resize_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, nw, nh
加入黑条(标签)
def letterbox_image(image, label, 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', [w, h], (128, 128, 128))
new_image.paste(image, ((w - nw) // 2, (h - nh) // 2))
label = label.resize((nw, nh), Image.NEAREST)
new_label = Image.new('RGB', [w, h], (0, 0, 0))
new_label.paste(label, ((w - nw) // 2, (h - nh) // 2))
return new_image,new_label
灰度加入边
def letterbox_image(image, label, size):
iw, ih = image.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', [w, h], (128, 128, 128))
new_image.paste(image, ((w - nw) // 2, (h - nh) // 2))
label = label.resize((nw, nh), Image.NEAREST)
new_label = Image.new('L', [w, h], (0))
new_label.paste(label, ((w - nw) // 2, (h - nh) // 2))
return new_image, new_label