1

I followed a tutorial, and tried to make the program quit when I press q, but that doesn't work, it quits no matter which key I press, that's the code:

twi = cv2.imread('large.png')
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', twi)
key = cv2.waitKey(0)
if key == ord('q'):
    cv2.destroyAllWindows()

And I've tried to add &0xFF:

key = cv2.waitKey(0)&0xFF

Still not working, what is wrong with it? Can I fix it?

=============Update============

I added print(key) to that, when pressing q, variable key is 113, and ord('q') is 113 either, can't understand why it doesn't work...

=============Solved=============

(Seems that I forget how if works...)

2
  • 2
    "it quits no matter what key I pressed" show us the rest of the code, does it quit right after that shown lines? Then it is clear why it quits, because the code enters the if statement, sees "oh it isn't 'q' so just go on" and then it reaches the end of your code and quits. Commented Jun 30, 2017 at 12:51
  • 2
    @Micka Gosh I must be stupid at the moment... Now I understand... Thank you! Commented Jun 30, 2017 at 12:52

1 Answer 1

6

Continuously calling cv2.waitKey(0) when it doesn't return the key value for the q key might work:

while cv2.waitKey(0) != ord('q'):
    pass
cv2.destroyAllWindows()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.