我试图使用opencvpython库使用cv2.imwrite()
将图像保存到文件中,但它显示了一个错误
代码是:
import cv2
# Reading image as gray scale
img = cv2.imread(r'C:\Users\Person\Pictures\Wallpapers (Space)\Carl Sagan.jpg',1)
# saving the image
status = cv2.imwrite(r'C:\Users\Person\Pictures\Wallpapers (Space)\Carl Sagan.jpg',0,img)
print('the status of the image is : ',status)
显示错误:
error Traceback (most recent call last)
<ipython-input-4-773a1fcb9cd4> in <module>
4 img = cv2.imread(r'C:\Users\Joydeep\Pictures\Wallpapers (Space)\Carl Sagan.jpg',1)
5 # saving the image
----> 6 status = cv2.imwrite(r'C:\Users\Joydeep\Pictures\Wallpapers (Space)\Carl Sagan.jpg',0,img)
7
8 print('the status of the image is : ',status)
error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'imwrite'
> Overload resolution failed:
> - Conversion error: params, what: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-inblc7p7\opencv\modules\core\src\copy.cpp:320: error: (-215:Assertion failed) channels() == CV_MAT_CN(dtype) in function 'cv::Mat::copyTo'
>
> - Conversion error: params, what: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-inblc7p7\opencv\modules\core\src\copy.cpp:320: error: (-215:Assertion failed) channels() == CV_MAT_CN(dtype) in function 'cv::Mat::copyTo'
>
***我该怎么办
首先,当使用
1
作为cv2.imread()
方法的第二个参数时,它将以全色而不是灰度读取图像;请注意1
是标志的默认值。要将图像作为灰度图像读入,请使用0
,这是cv2.IMREAD_GRAYSCALE
的值最后,在编写图像时不包括
0
:如果您喜欢以彩色读取图像,而只以灰度写入,则可以使用
cv2.cvtColor
方法:其中
6
是cv2.COLOR_BGR2GRAY
的值根据docs,
imwrite
具有以下参数:其中
params
是指定输出的ImwriteFlags所以可能是
0
,试试:相关问题 更多 >
编程相关推荐