縮放圖片至固定大小,尺寸不足以0填充

chyao7發表於2020-11-20

縮放圖片至固定大小,尺寸不足以0填充

def scale(img, long_size=192,short_size=48):
    h, w = img.shape[0:2]

    a = short_size - h
    b = long_size - w

    if a > 0:
        img = np.pad(img,((0,a),(0,0),(0,0)),"constant",constant_values=0)
    if b> 0:
        img = np.pad(img, ((0, 0), (0, b), (0, 0)), "constant", constant_values=0)
    h2, w2 = img.shape[0:2]
    scale = short_size * 1.0 / h2
    scale2 = long_size*1.0/w2
    img = cv2.resize(img, dsize=None, fx=scale2, fy=scale)
    print(img.shape)
    return img

相關文章