-2 opencv encoding_error ai_generated true

cv2.error: OpenCV(4.8.0) /modules/imgcodecs/src/loadsave.cpp:734: error: (-2:未指定的错误) 找不到指定扩展名的写入器 在函数'cv::imwrite_'中

cv2.error: OpenCV(4.8.0) /modules/imgcodecs/src/loadsave.cpp:734: error: (-2:Unspecified error) could not find a writer for the specified extension in function 'cv::imwrite_'

ID: opencv/imwrite-format-unsupported

其他格式: JSON · Markdown 中文 · English
93%修复率
86%置信度
1证据数
2023-02-28首次发现

版本兼容性

版本状态引入弃用备注
4.5.0 active
4.6.0 active
4.8.0 active

根因分析

输出路径中的文件扩展名不被OpenCV的图像写入编解码器识别或支持(例如,支持.bmp、.jpg、.png,但可能缺少.webp或.tiff)。

English

The file extension in the output path is not recognized or supported by OpenCV's image writing codecs (e.g., .bmp, .jpg, .png are supported, but .webp or .tiff may be missing).

generic

官方文档

https://docs.opencv.org/4.8.0/d4/d5e/group__imgcodecs.html

解决方案

  1. Use a supported extension like '.png' or '.jpg': `cv2.imwrite('output.jpg', img)`
  2. Rebuild OpenCV with the desired format support, e.g., for WEBP: `cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules -DBUILD_WEBP=ON ..`
  3. Use an external library like Pillow to convert the image to a supported format before saving with OpenCV.

无效尝试

常见但无效的做法:

  1. Installing libwebp-dev via apt but not rebuilding OpenCV with WEBP support. 70% 失败

    Installing additional image libraries without rebuilding OpenCV does not add codec support.

  2. Converting the image to float32 before saving. 90% 失败

    Changing the image data type does not affect codec availability.

  3. Converting the image to grayscale before saving. 80% 失败

    Using a different color space does not enable unsupported formats.