如何设置双循环以从左上角到右下角扫描图像?

时间:2021-07-02 18:37:17

标签: python image loops

我想从左上角到右下角逐行计算图片中的所有像素颜色。我只需要在行上每 5 个像素计算 1 个像素颜色,并且只计算 5 行中的 1 行。

count = 0
for y in range(height,height +5):
    for x in range(width,width+5):
         ''' some code here counting pixel colors '''
return count 
''' some other function that does height+=1, width +=1'''

您能帮忙吗,因为我的一段代码无法正常工作且无法正常扫描?

1 个答案:

答案 0 :(得分:0)

您应该像这样更改代码,以便从 0 开始 for 循环,直到图片大小,然后增加 +5 步:

count = 0
for y in range(0,height, +5):
    for x in range(0, width,+5):
         ''' some code here counting pixel colors '''
return count
''' some other function that does height+=1, width +=1'''

假设左上角是 (0,0) 屏幕像素