It seems really simple, but I can't get it to work and the couldn't find any questions regarding this particular issue (if there are, please point out in the comments).
I am showing an image and want the window to close on a specific key, but strangely, any key causes it to close.
This is my simple code for testing:
img = cv2.imread("MyImage.png")
cv2.imshow('My Image', img)
k = cv2.waitKey(0) & 0xFF
print(k)
if k == 27: # close on ESC key
cv2.destroyAllWindows()
(based on what is said here)
No matter what key I press, the key code is shown (27 for ESC, 32 for SPACE, ...) and the window closes.
The main problem: the if clause is never reached (I checked by putting the print(k) inside it, and nothing is printed). After the key press, the program simply stops running and it doesn't get to checking the key code.
(I am on macOS Catalina, with Python 3.8)
So, how do I actually make it wait for a specific key?