OpenCV imwrite报错:无法找到指定扩展名的写入器问题求助
I’ve run into this exact pitfall before—this error almost always boils down to issues with your save path/filename, missing format support in your OpenCV build, or invalid image data. Let’s walk through the fixes step by step:
1. Double-check your save filename extension
OpenCV relies on explicit file extensions (like .jpg, .png) to pick the right image encoder. If you’re saving to a path without a valid extension, it’ll throw this confusing error instead of telling you the real problem.
- Bad example:
cv2.imwrite("./target/10-0_marked", img)(no extension) - Good example:
cv2.imwrite("./target/10-0_marked.jpg", img)(clear.jpgextension) - Also watch out for case sensitivity:
.JPGmight work on Windows but fail on Linux/macOS—stick to lowercase extensions like.jpgor.pngfor cross-platform compatibility.
2. Ensure your target save directory exists and has write permissions
If the folder you’re trying to save to doesn’t exist, imwrite will fail silently and fall back to the "no writer" error. Always validate the directory first:
import os save_dir = "./your_target_folder" # Create directory if it doesn't exist, don't throw error if it does os.makedirs(save_dir, exist_ok=True)
Also confirm your user account has write access to the directory—on Linux/macOS, you might need to adjust permissions with chmod if you’re saving to a system folder.
3. Verify OpenCV supports your target image format
Sometimes OpenCV is compiled without support for common formats like JPEG or PNG. To check what formats your build supports, run this:
print(cv2.getBuildInformation())
Look for the Image I/O section in the output—you should see JPEG: YES, PNG: YES next to the formats you want to use. If any are marked NO, you’ll need to either:
- Recompile OpenCV with support for those formats, or
- Switch to a universally supported format like
.bmp(which works with almost all OpenCV builds)
4. Confirm your image data is in the correct format
After drawing rectangles, make sure your image is still a valid uint8 array (the default format for OpenCV images):
print(img.dtype) # Should output uint8 # If it's float32 or another type, convert it: img = img.astype(np.uint8)
Also, a quick note: you mentioned using (255,0,0) for red boxes—remember OpenCV uses BGR channel order, so (0,0,255) is actually red! Your current code is drawing blue boxes, which might not be what you intended.
Full Working Example Code
Here’s a polished version of your code incorporating all these fixes:
import cv2 import os import numpy as np # Your image path and bounding box data image_box_map = { "10-0": [[1915, 387, 1933, 402], [3350, 387, 3407, 391], [842, 505, 863, 521]] } # Target directory for marked images target_dir = "./marked_images" os.makedirs(target_dir, exist_ok=True) for img_id, boxes in image_box_map.items(): # Load original image (adjust extension to match your source files) source_img_path = f"{img_id}.jpg" img = cv2.imread(source_img_path) if img is None: print(f"Warning: Could not load image {source_img_path}") continue # Draw red bounding boxes (BGR format: 0,0,255) for box in boxes: x1, y1, x2, y2 = map(int, box) cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 2) # Save the marked image with valid extension save_path = os.path.join(target_dir, f"{img_id}_marked.jpg") save_success = cv2.imwrite(save_path, img) if save_success: print(f"Successfully saved marked image to {save_path}") else: print(f"Failed to save image to {save_path}")
内容的提问来源于stack exchange,提问作者user9266899

