使用Python计算黑色像素

2024-04-18 18:33:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我是第一次上编程课,所以很新。我在试着数一张照片中的黑色像素,但我被卡住了。到目前为止,我得到的是:

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

Tags: maindef编程showcreate像素照片black
1条回答
网友
1楼 · 发布于 2024-04-18 18:33:03

kedar和deets如何指出,您的返回值在for中,因此,在第一个像素中,它将返回countZero的值,而不是在整个图像上循环,只需修复缩进就可以了:

 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

相关问题 更多 >