0

I want to read the window screen and display it by using the cv2.imshow() method.

Right now I am taking ScreenShot of the window and displaying that on the OpenCV window but it is also showing itself which I don't want.

which other approach should I adopt to get my result?

This is the code I am using right now.

while True:
    img = screenshot()
    img = np.array(img)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    cv2.imshow("Test", img)

libraries, I am using are:

  1. pyautogui # for Screenshot()
  2. cv2 # for imshow()
  3. numpy # for array()

This is what I don't want to happen. Saved screenshot

https://i.stack.imgur.com/7PaC1.jpg

Code is taking Screenshot of imshow window as well but also I do not want to close or minimize the imshow window.

Q. Is there any other method to achive what I want?

Yash
  • 11
  • 6
  • so... you don't want the imshow window? *don't* use imshow then? – Christoph Rackwitz Jul 22 '21 at 18:10
  • https://stackoverflow.com/questions/10266281/obtain-active-window-using-python – devesh Jul 22 '21 at 18:23
  • 1
    Maybe you need to post some images of (1) what you're getting and (2) what you want. It sounds like maybe you want to [crop the image](https://stackoverflow.com/q/15589517/9705687). – bfris Jul 22 '21 at 23:40
  • Do you want to [minimize](https://stackoverflow.com/questions/45468876/minimize-opencv-highgui-window) the "Test" window before taking the screenshot? Destroying the window and recreating it after screenshot will also get you what you need. – PG11 Jul 23 '21 at 22:58
  • Christoph Rackwitz, I want the imshow window but I don't want it in the screenshot. – Yash Jul 24 '21 at 19:08
  • I have updated my question with more details and added Images to it. Please answer it if that's possible. – Yash Jul 24 '21 at 19:17

1 Answers1

0

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)
veedata
  • 1,048
  • 1
  • 9
  • 15
  • Thank you @veedata. Right Now I am already using `imwrite()` method for the working of my code. but I find Option 3 acceptable. I will try this. – Yash Jul 26 '21 at 11:47
  • Do note that the major caveat with option 3 is a relatively long delay to allow for the window to minimize before you take the screenshot. – veedata Jul 26 '21 at 12:21