1.
import cv2
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from matplotlib.image import imread
def letterbox_image(image, size):
ih, iw, _ = np.shape(image)
w, h = size
scale = min(w/iw, h/ih)
nw = int(iw*scale)
nh = int(ih*scale)
image = cv2.resize(image, (nw, nh))
new_image = np.ones([size[1], size[0], 3]) * 128
new_image[(h-nh)//2:nh+(h-nh)//2, (w-nw)//2:nw+(w-nw)//2] = image
return new_image
2
if __name__=="__main__":
img = imread('img/timg.jpg')
plt.subplot(1, 2, 1)
plt.imshow(img)
img1 = letterbox_image(img,(512,512))
im = Image.fromarray(img1.astype('uint8'))
plt.subplot(1, 2, 2)
plt.imshow(im)
plt.show() #显示图片
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)