在opencv中提供了resize
方法来进行图片的缩放,首先我们读取图像,并打印图像的信息
import cv2
img = cv2.imread("open.png", 1)
imgInfo = img.shape
print(imgInfo)
我们可以看到结果为(541, 627, 3)
,他表示的是图像的高为541,宽为627,颜色组成方式3表示bgr三个通道
我们分别获取到图像的高和宽,我们缩放50%,
img = cv2.imread("open.png", 1)
imgInfo = img.shape
print(imgInfo)
height = imgInfo[0] #获取图像的高
width = imgInfo[1] #获取图像的宽
mode = imgInfo[2] #获取图像的模式
dstHeight = int(height * 0.5) # 缩放后的高
dstWidth = int(width * 0.5) # 缩放后的宽
dst = cv2.resize(img, (dstWidth, dstHeight)) #进行缩放,img表示要对哪个图像缩放,后边的就是宽高参数
print(dst.shape)
cv2.imshow("old", img)
cv2.imshow("new", dst)
cv2.waitKey(0)
可以看到,缩小了50%