AI and LLM integration · Decision guide
cv2.imwrite rejects an array of data type 17, object or bool
Short answer
img data type = 17 is not supported means cv2.imwrite got a NumPy array whose dtype is object, which NumPy numbers 17. OpenCV 4.7 printed the number; 4.14 and 5.1 print object instead. Save each image of a batch separately, or cast pixel data with np.clip(...).astype(np.uint8). On OpenCV 4.x a bool mask fails the same way.
img data type = 17 is not supported means cv2.imwrite was handed a NumPy array whose dtype is object, which NumPy numbers 17. OpenCV 4.7 printed the number; newer releases print the word object instead. OpenCV's Python converter has no matrix depth for that dtype, so the call fails before any file is opened.
Two things hide behind the message, and a third looks similar. Print the array's shape and the type of its first element, then pick the case below.
Find your case
- If the shape is one number such as (16,) and the first element is itself an arrayYou are holding a batch, not a picture.
- If the shape looks like an image, for example (480, 640, 3), but dtype is objectThe pixel values are Python objects.
- If the message names bool and the array came from a comparison like gray > 127OpenCV 4.x cannot save a boolean mask.
See what the array really is
Put this just before the failing line. The shape and the first element tell the three cases apart immediately.
Version, type, dtype, shape and first element
print(cv2.__version__, type(img), img.dtype, img.shape)
print(type(img.flat[0]) if img.size else "empty array")A batch stored as an object array
Images of different sizes kept together with dtype=object produce an array of arrays. imwrite receives the container, not one picture. Loop over the batch and save each element on its own. When every image has the same size and dtype, np.stack can instead build one numeric array you can slice.
Save each frame separately
for i, frame in enumerate(img):
cv2.imwrite(f"frame_{i:04d}.png", frame)Pixels stored as Python objects
An array can have the height, width and channels of an image and still have dtype object, for instance when it was built from Python values that NumPy kept as objects. The imwrite documentation says that in general it saves 8-bit unsigned images, with exceptions for 16-bit and float data in some formats.
Convert the values to numbers, clip them to 0 to 255 and cast to uint8. A None hidden in the data makes the conversion raise a TypeError, which points at the step that produced it.
Inspect, convert, save
print(img.dtype, img.min(), img.max())
img = np.clip(img.astype(np.float32), 0, 255).astype(np.uint8)
cv2.imwrite("out.png", img)A boolean mask
In the 4.x line the Python converter accepts uint8, int8, 16-bit, int32, float16, float32 and float64 arrays, and casts 64-bit integers, but bool is not on the list. The 5.x converter added a bool depth, so the same call can succeed there. Map True and False to 255 and 0 so the mask saves as white shapes on black.
Convert, save, read back to confirm 255
print(cv2.__version__, mask.dtype)
cv2.imwrite("mask.png", mask.astype(np.uint8) * 255)
print(cv2.imread("mask.png", cv2.IMREAD_UNCHANGED).max())Guarding against it
If imwrite returns False without raising, the dtype is not the problem: check that the target folder exists and that the extension names a format OpenCV can write.
- Put
assert img.dtype == np.uint8next to eachimwriteso a wrong dtype fails with a clear line number. - Keep image batches as a Python list of arrays or one stacked uint8 array, never as an object array.
- Pin
opencv-pythonoropencv-python-headless, because 4.x and 5.x treat bool arrays differently.
In short
- Type 17 means dtype object, not a mysterious image format.
- Shape (N,) of arrays is a batch: loop. Image-shaped object array: cast to uint8.
- Booleans need a conversion on OpenCV 4.x.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.