python screenshot specific window

Solutions on MaxInterview for python screenshot specific window by the best coders in the world

showing results for - "python screenshot specific window"
Michele
31 Jan 2021
1import pyautogui
2import win32gui
3
4def screenshot(window_title=None):
5    if window_title:
6        hwnd = win32gui.FindWindow(None, window_title)
7        if hwnd:
8            win32gui.SetForegroundWindow(hwnd)
9            x, y, x1, y1 = win32gui.GetClientRect(hwnd)
10            x, y = win32gui.ClientToScreen(hwnd, (x, y))
11            x1, y1 = win32gui.ClientToScreen(hwnd, (x1 - x, y1 - y))
12            im = pyautogui.screenshot(region=(x, y, x1, y1))
13            return im
14        else:
15            print('Window not found!')
16    else:
17        im = pyautogui.screenshot()
18        return im
19
20
21im = screenshot('Calculator')
22if im:
23    im.show()
24