pythonnumpydtype

How to create a numpy array for a black/white image?


If we want to create a numpy array from scratch for a RGB image, we can use arr = numpy.zeros([height, width, 3], dtype=numpy.uint8)

But what dtype should be used to do the same for a black white image, i.e. the pixel value is either True or False?


Solution

  • May be you can try this (np.bool was a deprecated alias for the builtin bool. To avoid this error in existing code, use bool by itself. Doing this will not modify any behavior and is safe.)

    import numpy as np
    
    height, width = 100, 100
    bw_image = np.zeros([height, width], dtype=bool)
    print(bw_image)
    

    bool can be used as the dtype to represent a black and white image where each pixel can have values True (1) or False (0).