opencv与pillow(PIL) 图像补边

opencv 补边操作

import cv2

import numpy as np

img = cv2.imread(img_path)

h, w, _ = img.shape

size = max(h, w)

img_ = np.ones((size, size), dtype=np.uint8)

img_ = cv2.cvtColor(img_, cv2.COLOR_GRAY2BGR)

img_[0:h, 0:w] = img

PILLOW补边操作

from PIL import Image

img_=Image.open(img_path)

w, h, _ = img_.size

size = max(h, w)

img_ = Image.fromarray(img_)

img = Image.new("RGB", (size,size))        

img.paste(img_, (0, 0))

你可能感兴趣的:(opencv,pillow)