-2 opencv config_error ai_generated partial

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

ID: opencv/imgcodecs-imwrite-extension-unsupported

Also available as: JSON · Markdown · 中文
85%Fix Rate
84%Confidence
1Evidence
2023-06-18First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
4.5.5 active
4.6.0 active
4.7.0 active
4.8.0 active
4.9.0 active

Root Cause

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

中文

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

Official Documentation

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

Workarounds

  1. 95% success 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.
    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. 80% success Rebuild OpenCV from source with the required image format support: cmake -D WITH_WEBP=ON -D BUILD_JPEG=ON -D BUILD_TIFF=ON ..
    Rebuild OpenCV from source with the required image format support: cmake -D WITH_WEBP=ON -D BUILD_JPEG=ON -D BUILD_TIFF=ON ..
  3. 90% success 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')
    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. 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')

Dead Ends

Common approaches that don't work:

  1. 70% fail

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

  2. 90% fail

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