My personal preference would be to use cv2.imwrite
instead of cv2.imshow
. but if you requirement needs you to use imshow, you can check out these 2 methods and see which fits your requirements
Option 1: Destroy the window before you take the screenshot and then make it again, the code for that would look like:
while True:
img = screenshot()
img = np.array(img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imshow("Test", img)
cv2.destroyAllWindows()
I personally do not see a lot of merit to this method as it will majorly just keep creating and destroying windows
Option 2: OpenCV also allows you to move the window, you can use that to move the window before you are about to take the screenshot and then move it back in afterwards. The code for the same would look like:
while True:
# Just to check if img exists or not, needed for the 1st run of the loop
if 'img' in locals():
cv2.waitKey(100) #Without the delay, the imshow window will only keep flickering
cv2.moveWindow("Test", height, width)
img = screenshot()
img = np.array(img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
height, width, ch = img.shape
cv2.imshow("Test", img)
cv2.moveWindow("Test", 0, 0)
The above 2 options were using the libraries that you already are using in your code. There is a third option too where you can minimize the window and then reopen it each time that you take a screenshot. You can find references to it over here, and here. And the code for the same should look like.
import ctypes
import win32gui
while True:
# Just to check if img exists or not,
# needed for the 1st run of the loop
if 'img' in locals():
cv2.waitKey(500) # Delay to stop the program from constantly opening and closing the window after itself
ctypes.windll.user32.ShowWindow(hwnd, 7)
# Window needs some time to be minimised
cv2.waitKey(500)
img = pyautogui.screenshot()
img = np.array(img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imshow("Test", img)
hwnd = win32gui.GetForegroundWindow()
ctypes.windll.user32.ShowWindow(hwnd, 9)