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]))
Share:
18,840
IvanovAndrew
Author by

IvanovAndrew

Updated on June 11, 2022

Comments

  • IvanovAndrew
    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
      Warren Weckesser over 9 years
      Why did you use dtype=type(RED) for image? type(RED) is a python list; using that makes image an array of python objects (and not an with a numeric type). Why not use, say, dtype=int?
    • IvanovAndrew
      IvanovAndrew over 9 years
      If I use dtype=type(int) (and RED = 200) then I will get the same error
    • Warren Weckesser
      Warren Weckesser over 9 years
      Ue dtype=int (not type(int)). Setting the dtype sets the data type of the elements of the array that you are creating.
    • Warren Weckesser
      Warren Weckesser over 9 years
      Also, if you are trying to create an array that holds RGB components, the shape should be (IMAGE_SIZE, IMAGE_SIZE, 3). E.g image = 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 use dtype=np.uint8.