0

so I got the following code:

import time
from cv2 import cv2

time.sleep(1)
image = cv2.imread("test.png", cv2.IMREAD_GRAYSCALE) 
cv2.imshow("image", image)
cv2.setWindowProperty("image", cv2.WND_PROP_TOPMOST, 1)
cv2.waitKey(0)

The program waits for 1 sec and after, it shows the image to me as top most window. When i press any key, the image and the program close. all good so far

but, if i press alt-tab while the program is sleeping(1) (i need to see the image while the focus is on a different window) this is what happens:

The program waits for 1 sec and then, it shows the image to me as top most window. When i press any key nothing happens. if I select/set focus on the image manually and then press any key it closes

i tried this http://www.noah.org/wiki/OpenCV_display_window_on_top_with_focus which describes a similar problem but didn't seem to solve the issue.

so i was wandering if there is a way to activate/set focus the window programmaticaly so when i press any key in the second scenario the window to close without manyally selecting it?

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Stelix
  • 9
  • 2

1 Answers1

0

For Windows OS you may use the solution described here:

import time
import cv2
from pywinauto.findwindows import find_window
from win32gui import SetForegroundWindow

time.sleep(5)
image = cv2.imread("test.png", cv2.IMREAD_GRAYSCALE) 
cv2.imshow("image", image)
cv2.setWindowProperty("image", cv2.WND_PROP_TOPMOST, 1)

SetForegroundWindow(find_window(title="image"))

cv2.waitKey()
cv2.destroyAllWindows()

For Linux you probably have to check how OpenCV was built (built with GTK or Qt) and research how to set the window to be in focus.

Rotem
  • 30,366
  • 4
  • 32
  • 65