如何计算png中黑色像素的数量?

问题描述 投票:0回答:2

我有一个在线绘制的黑白图形,因此像素only是纯黑色或纯白色。该图是png,我正在用python分析。

im = Image.open(os.path.join(dir))
im = img_as_float(im)
plt.imshow(im)

有人对如何计算png中黑色像素的数量有任何建议吗?

python graphics pixel scikit-image
2个回答
0
投票

假设所有像素都是黑色或白色,这应该可以:

len([px for px in list(im.getdata()) if px[1] < 0.01])


0
投票

简单的脚本来计算黑色像素:

def countBlack (image):
    blacks = 0
    for color in image.getdata():
        if color < 0.0001:
            blacks += 1
    return blacks
© www.soinside.com 2019 - 2024. All rights reserved.