📜  更改图像的大小并将其转换为 numpy 数组 opencv - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:21.562000             🧑  作者: Mango

代码示例1
# image variable is the image we are manipulating

output_img = np.ones((300, 300, 1)) * 128
output_img = (output_img).astype('uint8')

scale = 300 / (image.shape[0] * 1.0)
image_resize = cv2.resize(image, (0, 0), fx=scale, fy=scale, interpolation=cv2.INTER_LANCZOS4)

img_w = image_resize.shape[1]

if img_w < 300:
    #pad the image with values and make it 1:1 aspect ratio
    offset = img_w % 2
    output_img[:, int(300 / 2 - math.floor(img_w / 2)):int(300 / 2 + math.floor(img_w / 2) + offset), :] = image_resize

else:
    #crop the center of the image to maintain 1:1 aspect ratio
    output_img = image_resize[:,int(img_w / 2 - 300 / 2):
                  int(img_w / 2 + 300 / 2), :]