def find_centroid_faster(im, rez):
    width, height = im.size
XX, YY, count = 0, 0, 0
for x in range(0, width, rez):
    for y in range(0, height, rez):
        if im.getpixel((x, y)) == 255:
            XX += x
YY += y
count += 1
return XX / count, YY / count
Finding a specific index in a binary image in linear time?