Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing a window in Python

I'm currently looking to find a module, or code that would allow me to capture another processes window.

I've tried working with ImageGrab, however that just captures an area of the screen rather than binding to a specific process window. Since I'm working with a small monitor I can't guarantee that something won't lap over onto the captured area of the screen, so sadly the ImageGrab solution isn't good enough.

like image 849
SpiderStryder Avatar asked May 29 '26 11:05

SpiderStryder


1 Answers

You can achieve this using win32gui.

from PIL import ImageGrab
import win32gui

hwnd = win32gui.FindWindow(None, r'Window_Title')
win32gui.SetForegroundWindow(hwnd)
dimensions = win32gui.GetWindowRect(hwnd)

image = ImageGrab.grab(dimensions)
image.show()

You could also move the window to a preferred position if a small screen is the problem. win32gui.MoveWindow(hwnd, 0, 0, 500, 700, True) see win32gui.MoveWindow

like image 136
RottenCandy Avatar answered Jun 02 '26 03:06

RottenCandy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!