TypeError: img data type = 17 is not supported
18,840
dtype = type(RED)
gives you type list
and not type int
.
you need:
image = np.empty([IMAGE_SIZE, IMAGE_SIZE, 3], dtype=type(RED[0]))
Author by
IvanovAndrew
Updated on June 11, 2022Comments
-
IvanovAndrew almost 2 years
I create red image and try to save it via cv2.imwrite
import numpy as np import cv2 RED = [0, 0, 255] IMAGE_SIZE = 100 image = np.empty([IMAGE_SIZE, IMAGE_SIZE], dtype=type(RED)) for i in range(IMAGE_SIZE): for j in range(IMAGE_SIZE): image[i, j] = RED cv2.imwrite("red.png", image)
But I get error
File "C:/Users/Andrew/Desktop/Programms/image-processing-cource/Tracks.py", line 11, in save_image cv2.imwrite(name, image) TypeError: img data type = 17 is not supported
How to fix it?
Thanks!
-
Warren Weckesser over 9 yearsWhy did you use
dtype=type(RED)
forimage
?type(RED)
is a python list; using that makesimage
an array of python objects (and not an with a numeric type). Why not use, say,dtype=int
? -
IvanovAndrew over 9 yearsIf I use
dtype=type(int)
(andRED = 200
) then I will get the same error -
Warren Weckesser over 9 yearsUe
dtype=int
(nottype(int)
). Setting thedtype
sets the data type of the elements of the array that you are creating. -
Warren Weckesser over 9 yearsAlso, if you are trying to create an array that holds RGB components, the shape should be
(IMAGE_SIZE, IMAGE_SIZE, 3)
. E.gimage = np.empty([IMAGE_SIZE, IMAGE_SIZE, 3], dtype=int)
. If you want the elements to be, say, 8 bit unsigned integers (a typical size for images), you would usedtype=np.uint8
.
-