原文链接:http://www.juzicode.com/archives/5375
错误提示:
opencv imwrite()写入图像时提示error: (-2:Unspecified error) could not find a writer for the specified extension in function ‘cv::imwrite_’
import sys
import cv2
print('VX公众号: 桔子code / juzicode.com')
print('cv2.__version__:',cv2.__version__)
img = cv2.imread('..\\lena.jpg')
#print('img:',img)
print('type(img):',type(img))
if img is None:
print('读出的图像为空')
sys.exit(-1)
cv2.imwrite('lena.bmp',img)
cv2.imwrite('lena.png',img)
cv2.imwrite('lena.txt',img)
==========运行结果:
VX公众号: 桔子code / juzicode.com
cv2.__version__: 4.5.2
type(img): <class 'numpy.ndarray'>
Traceback (most recent call last):
File "read-write.py", line 21, in <module>
cv2.imwrite('lena.txt',img)
cv2.error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-vi271kac\opencv\modules\imgcodecs\src\loadsave.cpp:650: error: (-2:Unspecified error) could not find a writer for the specified extension in function 'cv::imwrite_'
错误原因:
1、cv2.imwrite(‘lena.txt’,img)一行写入的文件名称为lena.txt,opencv会根据文件后缀名称推断图像要保持的格式,但是并不存在txt类型的图像格式,所以报错。
扩展内容:
如果本文还没有完全解决你的疑惑,你也可以在微信公众号“桔子code”后台给我留言,欢迎一起探讨交流。