python cv2 调整图片大小
时间: 2023-11-20 20:07:48 浏览: 91
当我们使用 OpenCV 的 Python 接口时,可以使用 cv2.resize() 函数来调整图像的大小。该函数的语法如下:
cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])
其中,src 表示原始图像,dsize 表示输出图像的大小,fx 和 fy 表示水平和垂直方向的缩放比例,interpolation 表示插值方法。
例如,如果我们想将一张 640x480 的图像缩小到一半,可以这样写:
```
import cv2
img = cv2.imread('image.jpg')
resized_img = cv2.resize(img, (320, 240))
cv2.imshow('Resized Image', resized_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
这里将原始图像的大小指定为 (640, 480),输出图像的大小指定为 (320, 240),即将原始图像缩小到一半。如果我们想将图像放大到原来的两倍,可以这样写:
```
import cv2
img = cv2.imread('image.jpg')
resized_img = cv2.resize(img, None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
cv2.imshow('Resized Image', resized_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
这里将输出图像的大小指定为 None,然后将水平和垂直方向的缩放比例都设置为 2,即将原始图像放大到原来的两倍。插值方法使用了线性插值(cv2.INTER_LINEAR)。
阅读全文
相关推荐


















