Dear all, I have a MacBook Air Sierra 10.12.4, Python 2.7.13 installed with Homebrew and OpenCV 3.2.0. I would like to show two superposed images using the command cv2.addWeighted and random.uniform() in a while loop. I wrote the following code:
import random
import cv2
img1 = cv2.imread('img1.jpg')
img2 = cv2.imread('img2.jpg')
cv2.startWindowThread()
cv2.imshow('test',cv2.resize(img1,(1440,900)))
#1440,900 is the resolution of my laptop
while True :
alpha = random.uniform(0,1)
wei = cv2.addWeighted(img2,alpha,img1,1-alpha,0)
cv2.imshow('test',cv2.resize(wei,(1440,900)))
cv2.waitKey(100)
My problem is how to exit from the loop e.g. hitting the Esc key. I tried by including in the while loop
key=cv2.waitKey(0)
if (key==27):
cv2.destroyAllWindows()
for i in range (1,5):
cv2.waitKey(1)
(I found on the web that you need five times "cv2.waitKey(1)" to destroy effectively the window), but it does not work. The only way to interrupt the code is by clicking on the terminal window and hitting Ctrl+c.
To be more precise, I would like to show these two alternating images in fullscreen mode. This is obtained by adding the following two lines after the "cv2.startWindowThread()" command:
cv2.namedWindow('test',cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty('test',cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
The further problem in this case is that I cannot interrupt the script, because I cannot see and select the terminal window: keyboard is frozen as I can see only the mouse pointer but without being able to click.
The only way to stop everything... is rebooting my mac, so I am doing something terribly wrong, of course.
I hope to have been clear and thank you in advance for any suggestions.