简体   繁体   English

使用Python计算黑色像素

[英]Counting black pixels using Python

I am in my first programming class so very new. 我是第一个编程课,所以非常新。 I'm trying to count the black pixels in a picture and I'm stuck. 我正在尝试计算图片中的黑色像素而且我被卡住了。 This is what I have so far: 这是我到目前为止:

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

How it was pointed by kedar and deets , your return is INSIDE the for, so, in the first pixel it will return the value of countZero, instead of looping over all the image, just fixing the indention should be fine : 如何通过kedar和deets指出,你的返回是INSIDE 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM