且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何计算python中某种颜色的像素数?

更新时间:2023-02-13 18:58:28

我已将代码从0xd3更正为实际可用:

I corrected code from 0xd3 to actually work:

from PIL import Image
im = Image.open('black.jpg')

black = 0
red = 0

for pixel in im.getdata():
    if pixel == (0, 0, 0): # if your image is RGB (if RGBA, (0, 0, 0, 255) or so
        black += 1
    else:
        red += 1
print('black=' + str(black)+', red='+str(red))