If I want to have an array filled whith 0 or 1 depending of the pixels value in a image, I write this :
image = "example.jpg"
imageOpen = Image.open(image)
bwImage = imageOpen.convert("1", dither=Image.NONE)
bw_np = numpy.asarray(bwImage)
print(type(bw_np[0, 0]))
Result :
<class 'numpy.bool_'>
Because of the .convert bilevel mode "1", the array must be full of 1 and 0. https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.convert
When I try something simpler :
bw_np = numpy.asarray([0, 1])
print(type(bw_np[0]))
Result :
<class 'numpy.int32'>
But instead of the second example, the first is full of true and false.
So Why ?
In a nutshell : In python
Trueis1andFalseis0. This should correct this weird behavior :Long answer : Maybe
imageOpen.convert("1", dither=Image.NONE)prefer bool instead of int32 for a better memory management :Result :