import cv2 import numpy as np from PIL import Image,ImageOps import copy img = cv2.imread('F:\\2\\images.jpg') img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) print(img.shape) img2=copy.deepcopy(img) constant = cv2.copyMakeBorder(img, 0, 0, 50, 100, cv2.BORDER_CONSTANT, value=0)#top,bottom,left,right cv2.imshow('pistol', constant) print(constant.shape) cv2.waitKey(0) constant2=Image.fromarray(img2) print(img2.shape) constant2=ImageOps.expand(constant2, border=(50,0,100,0), fill=0)##left,top,right,bottom constant2=np.asarray(constant2) print(constant2.shape) print(np.sum(constant2-constant))
(179, 281)
(179, 431)
(179, 281)
(179, 431)
0