'Why the loop is infinite?

When I use this code, it runs infinite loop, and the image which I want to show it ,does not respond and appear as a gray picture

    h,w=Img.size
    for x in range(h):
        for y in range (w):
            r, g, b = Img.getpixel((x, y))
            a = (r, g, b)
            print (a)
    cv2.imshow("Mark Zukerberg", Img)


Solution 1:[1]

The loop isn't actually infinite, it is just a very long, large, process, so it will take a while to finish. If you were to have it print y and/or x every time it iterates, you can see it is incrementing, just more slowly then wanted. I would recommend using a compiled language, such as C++, Java, or Go for loops this large, as they will run faster.

Solution 2:[2]

print actually takes more resources than you would expect, so that might be the issue. Try removing the print function.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 alien_jedi
Solution 2 acornTime