import os
import cv2
BLACK = [158,160,161]
for filename in os.listdir(r"./0"):
filename_ori = './0/'+filename
image=cv2.imread(filename_ori)
shape = image.shape
print(shape)
r0 = shape[0]/224
r1 = shape[1]/224
if r0>r1:
width = int(shape[1]/r0)
height = 224
else:
width = 224
height = int(shape[0]/r1)
print(height)
print(width)
res=cv2.resize(image,(width,height),interpolation=cv2.INTER_CUBIC)
if r0>r1:
pading = int((224 - width)/2)
constant = cv2.copyMakeBorder(res,0,0,pading,pading,cv2.BORDER_CONSTANT,value= BLACK)
else:
pading = int((224 - height)/2)
constant = cv2.copyMakeBorder(res,pading,pading,0,0,cv2.BORDER_CONSTANT,value= BLACK)
out_filename = './0_out/'+filename
cv2.imwrite(out_filename, constant)
图像等比例缩放加pading
最新推荐文章于 2024-12-26 17:03:31 发布