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

- **ID:** `opencv/imgcodecs-imwrite-extension-unsupported`
- **领域:** opencv
- **类别:** config_error
- **错误码:** `-2`
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 4.5.5 | active | — | — |
| 4.6.0 | active | — | — |
| 4.7.0 | active | — | — |
| 4.8.0 | active | — | — |
| 4.9.0 | active | — | — |

## 解决方案

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')
   ```

## 无效尝试

- **** — The extension determines the codec used; a mismatch between extension and actual format may produce a corrupted file. (70% 失败率)
- **** — OpenCV must be compiled with support for these libraries; system libraries alone do not enable the codec. (90% 失败率)
