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

119 Views Asked by At

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?

2

There are 2 best solutions below

1
Prudhviraj Panisetti On BEST ANSWER

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).

1
Meaningful Life On

If you would like to create a numpy array for a black and white image you just need to add dtype=bool

height, width = 100, 100
image = np.zeros([100, 100], dtype=bool)