Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I cannot exit from while loop while showing images (python 2.7.13/OpenCV 3.2.0)

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.

I cannot exit from while loop while showing images (python 2.7.13/OpenCV 3.2.0)

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.

EDIT

Thanks to LBerger suggestion, I put the while loop inside a function so, the new version of the code is:

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

def main() :
    while True :

        alpha = random.uniform(0,1)
        wei = cv2.addWeighted(img2,alpha,img1,1-alpha,0)
        cv2.imshow('test',cv2.resize(wei,(1440,900)))
        ch = cv2.waitKey(1)
        if ch == 27:
            break
        cv2.waitKey(100)

if __name__ == '__main__':
    main()

cv2.destroyAllWindows()
cv2.waitKey(1)

But I have still a problem. If I add the two lines to display the images fullscreen as explained above (using cv2.WND_PROP_FULLSCREEN / cv2.WINDOW_FULLSCREEN) after "cv2.startWindowThread()", when I press the ESC key, the two images stop alternating, so I think we are out of the while loop, but the fullscreen window does not close! Everything is frozen again... and I have to reboot the computer. Any other suggestions? Thank you again.

I cannot exit from while loop while showing images (python 2.7.13/OpenCV 3.2.0)

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.

EDIT

Thanks to LBerger suggestion, I put the while loop inside a function so, the new version of the code is:

import random
import cv2

img1 = cv2.imread('img1.jpg')
img2 = cv2.imread('img2.jpg')

cv2.startWindowThread()
# cv2.namedWindow('test',cv2.WND_PROP_FULLSCREEN)
# cv2.setWindowProperty('test',cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN) 
cv2.imshow('test',cv2.resize(img1,(1440,900)))

#1440,900 is the resolution of my laptop

def main() :
    while True :

        alpha = random.uniform(0,1)
        wei = cv2.addWeighted(img2,alpha,img1,1-alpha,0)
        cv2.imshow('test',cv2.resize(wei,(1440,900)))
        ch = cv2.waitKey(1)
        if ch == 27:
            break
        cv2.waitKey(100)

if __name__ == '__main__':
    main()

cv2.destroyAllWindows()
cv2.waitKey(1)

But I But I still have still a problem. If I add the two commented lines after "cv2.startWindowThread()" to display the images fullscreen as explained above (using cv2.WND_PROP_FULLSCREEN / cv2.WINDOW_FULLSCREEN) after "cv2.startWindowThread()", fullscreen, when I press the ESC key, the two images stop alternating, so I think we are out of the while loop, but the fullscreen window does not close! Everything is frozen again... again, the keyboard "beeps" when pressing any key... and I have to reboot the computer. Any other suggestions? Thank you again.Does anyone have the same problem running the above script (uncommenting the two lines for fullscreen mode? Thanks in advance.

I cannot exit from while loop "while loop" while showing images (python 2.7.13/OpenCV 3.2.0)

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.

EDIT

Thanks to LBerger suggestion, I put the while loop inside a function so, the new version of the code is:

import random
import cv2

img1 = cv2.imread('img1.jpg')
img2 = cv2.imread('img2.jpg')

cv2.startWindowThread()
# cv2.namedWindow('test',cv2.WND_PROP_FULLSCREEN)
# cv2.setWindowProperty('test',cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN) 
cv2.imshow('test',cv2.resize(img1,(1440,900)))

#1440,900 is the resolution of my laptop

def main() :
    while True :

        alpha = random.uniform(0,1)
        wei = cv2.addWeighted(img2,alpha,img1,1-alpha,0)
        cv2.imshow('test',cv2.resize(wei,(1440,900)))
        ch = cv2.waitKey(1)
        if ch == 27:
            break
        cv2.waitKey(100)

if __name__ == '__main__':
    main()

cv2.destroyAllWindows()
cv2.waitKey(1)

But I still have a problem. If I add the two commented lines after "cv2.startWindowThread()" to display the images fullscreen, when I press the ESC key, the two images stop alternating, so I think we are out of the while loop, but the fullscreen window does not close! Everything is frozen again, the keyboard "beeps" when pressing any key... and I have to reboot the computer. Does anyone have the same problem running the above script (uncommenting the two lines for fullscreen mode? Thanks in advance.