-2 opencv config_error ai_generated partial

cv::error: (-2:未指定错误) 在函数 'imwrite_' 中找不到指定扩展名的写入器

cv::error: (-2:Unspecified error) could not find a writer for the specified extension in function 'imwrite_'

ID: opencv/imgcodecs-imwrite-extension-unsupported

其他格式: JSON · Markdown 中文 · English
85%修复率
84%置信度
1证据数
2023-06-18首次发现

版本兼容性

版本状态引入弃用备注
4.5.5 active
4.6.0 active
4.7.0 active
4.8.0 active
4.9.0 active

根因分析

提供给 cv2.imwrite 的图像文件扩展名不受当前 OpenCV 构建支持(例如 .webp、.jp2、.tiff 缺少相应的编解码器支持)。

English

The image file extension provided to cv2.imwrite is not supported by the OpenCV build (e.g., .webp, .jp2, .tiff without proper codec support).

generic

官方文档

https://docs.opencv.org/4.x/d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce

解决方案

  1. Convert the image to a supported format before saving: if extension not in ['.png', '.jpg', '.jpeg', '.bmp']: cv2.imwrite('output.png', img); then rename or convert with another tool.
  2. Rebuild OpenCV from source with the required image format support: cmake -D WITH_WEBP=ON -D BUILD_JPEG=ON -D BUILD_TIFF=ON ..
  3. Use a different library like PIL to save in unsupported formats: from PIL import Image; Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)).save('output.webp')

无效尝试

常见但无效的做法:

  1. 70% 失败

    The extension determines the codec used; a mismatch between extension and actual format may produce a corrupted file.

  2. 90% 失败

    OpenCV must be compiled with support for these libraries; system libraries alone do not enable the codec.