12

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.

Miki
  • 40,887
  • 13
  • 123
  • 202
Cuo Show
  • 235
  • 1
  • 3
  • 11
  • 2
    You could detect what window you want to capture and find it's height, width, and coords... – MooingRawr Nov 25 '16 at 17:09
  • 1
    @MooingRawr Thanks for your idea. But how? I originally thought there may be a method I can provide a window title or program ID – Cuo Show Nov 26 '16 at 02:30

2 Answers2

31

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)`

alpha_989
  • 4,882
  • 2
  • 37
  • 48
8

A example to get the content(screenshot) of a specific window with the help of the pygetwindow module.

import pygetwindow
import time
import os
import pyautogui
import PIL

# get screensize
x,y = pyautogui.size()
print(f"width={x}\theight={y}")

x2,y2 = pyautogui.size()
x2,y2=int(str(x2)),int(str(y2))
print(x2//2)
print(y2//2)

# find new window title
z1 = pygetwindow.getAllTitles()
time.sleep(1)
print(len(z1))
# test with pictures folder
os.startfile("C:\\Users\\yourname\\Pictures")
time.sleep(1)
z2 = pygetwindow.getAllTitles()
print(len(z2))
time.sleep(1)
z3 = [x for x in z2 if x not in z1]
z3 = ''.join(z3)
time.sleep(3)

# also able to edit z3 to specified window-title string like: "Sublime Text (UNREGISTERED)"
my = pygetwindow.getWindowsWithTitle(z3)[0]
# quarter of screen screensize
x3 = x2 // 2
y3 = y2 // 2
my.resizeTo(x3,y3)
# top-left
my.moveTo(0, 0)
time.sleep(3)
my.activate()
time.sleep(1)

# save screenshot
p = pyautogui.screenshot()
p.save(r'C:\\Users\\yourname\\Pictures\\\\p.png')

# edit screenshot
im = PIL.Image.open('C:\\Users\\yourname\\Pictures\\p.png')
im_crop = im.crop((0, 0, x3, y3))
im_crop.save('C:\\Users\\yourname\\Pictures\\p.jpg', quality=100)

# close window
time.sleep(1)
my.close()
martineau
  • 119,623
  • 25
  • 170
  • 301
tinus
  • 149
  • 1
  • 7
  • Looks good. However, instead of taking a screenshot of the full screen and cutting out the desired beforehand activated, resized and repositioned window, isn't there a way to directly get only the content of the desired window by name (maybe even when it is partly or fully covered by other windows, and maybe even without putting it into front)? – theozh Feb 01 '23 at 10:10