slomil - 7 months ago 41

Python Question

I am working with pixels and I want to find location of first black pixel in picture below when traversing in row-major order. This is a part of project to recognize mnist numbers on image and count sum of all numbers on image.

Code looks like:

`for a in range(0,80):`

for b in range(0,80):

if (imgt[a,b,0]==0 and imgt[a,b,1]==0 and imgt[a,b,2]==0):

x=a

y=b

break

break

break

print x,y

`range`

`x`

`a`

`y`

`b`

Does anyone know what can be problem here? It seems to me very simple, but i don't know what to do.

Thanks in advance!

Answer

The problem arose from use of multiple break statements. Moving the code to a separate function and use of return instead of break statements solves it.

```
def glteme():
for a in range(0,80):
for b in range(0,80):
if (imgt[a,b,0]==0 and imgt[a,b,1]==0 and imgt[a,b,2]==0):
return a,b
blackPixelCoordinate = glteme()
print blackPixelCoordinate[0], blackPixelCoordinate[1]
```

Source (Stackoverflow)