如果我想根据图像中的像素值,用0
或1
填充一个数组,我可以这样写:
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]))
结果:
<class 'numpy.bool_'>
由于.convert
双层模式"1"
,数组中必须充满1
和0
。https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.convert
当我尝试更简单的方法时:
bw_np = numpy.asarray([0, 1])
print(type(bw_np[0]))
结果:
<class 'numpy.int32'>
但是与第二个例子不同,第一个例子充满了true
和false
。
那为什么呢?你知道吗
简而言之:在python中
True
是1
,False
是0
。这应该可以纠正这种奇怪的行为:长话短说:为了更好的内存管理,也许
imageOpen.convert("1", dither=Image.NONE)
更喜欢bool而不是int32:结果:
相关问题 更多 >
编程相关推荐