Python错误集锦:OpenCV-Python imwrite()写入图像时提示error: (-2:Unspecified error) could not find a writer for the specified extension in function ‘cv::imwrite_’

原文链接: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类型的图像格式,所以报错。

 

扩展内容:

  1.  OpenCv-Python教程–获取、显示、写入
  2. 数据可视化~matplotlib显示二维图像

 


 

如果本文还没有完全解决你的疑惑,你也可以在微信公众号“桔子code”后台给我留言,欢迎一起探讨交流。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注