Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can python get the screen shot of a specific window?

Tags:

python

For example, I am running a java program. Can I use python to get the content(screenshot) of this java program? Not the full screen, just the java program.

I have seen this module, but it required a parameter of "where the program window is":

import pyscreenshot as ImageGrab

if __name__ == "__main__":
    # part of the screen
    im=ImageGrab.grab(bbox=(10,10,510,510)) # X1,Y1,X2,Y2
    im.show()
#-#

But this may not be what I am seeking since it requires the bounding_box.

like image 209
Cuo Show Avatar asked Nov 25 '16 17:11

Cuo Show


1 Answers

There are multiple ways to do that. The code for these will require modification based on your needs, so I will put in some pointers to the high level methods and libraries:

  1. You can directly use GUI-automation tools such as Pyautogui to press Alt+PrntScreen and transfer the data from clipboard into a variable (http://pyautogui.readthedocs.io/en/latest/keyboard.html#keyboard-keys)

  2. You can use pywin32 and PIL to grab the image of a certain window (How to Get a Window or Fullscreen Screenshot in Python 3k? (without PIL))

  3. You can use imagemagik to grab screenshots of a single window (https://www.imagemagick.org/discourse-server/viewtopic.php?t=24702)

  4. You can use Pyautogui.locateOnScreen(“Sceenshot.PNG”) to find the tuple which will contain the boundaries of the window for your java app. Then you can pass it to your functionimg.ImageGrab.grab(bbox)`

like image 192
alpha_989 Avatar answered Oct 25 '22 22:10

alpha_989