使用Python计算黑色像素

时间:2015-03-05 14:17:37

标签: python pixels

我在我的第一个编程课程中非常新。我正在尝试计算图片中的黑色像素而且我被卡住了。这就是我到目前为止所做的:

def main():
#create a 10x10 black picture
 newPict = makeEmptyPicture(10, 10)
 show(newPict)
 setAllPixelsToAColor(newPict,black)
 #Display the picture
 show(newPict)
 #Initialize variabl countBlack to 0 
 countZero = 0
 for p in getPixels(newPict):
     r = getRed(p)
     b = getBlue(p)
     g = getGreen(p)
     if (r,g,b) == (0,0,0):
        countZero = countZero + 100
     return countZero

1 个答案:

答案 0 :(得分:0)

kedar和deets如何指出,你的返回是INSIDE,因此,在第一个像素中它将返回countZero的值,而不是循环遍历所有图像,只需修复缩进应该没问题:< / p>

 for p in getPixels(newPict):
     r = getRed(p)
     b = getBlue(p)
     g = getGreen(p)
     if (r,g,b) == (0,0,0):
        countZero = countZero + 1
 return countZero