For a sample picture, the value of start should be 7 after execution, but gives output 0 when run as script. When debugged, it returns the correct result. def startdetection(start): i = 0 while( i < column): pixel = image.getpixel((i,start))[0] if pixel ==0: return elif i == column-1: start = start +1 i = 0 else: i = i+1 end = start + 38 startdetection(0) print(start) Continue reading...