capture_specific_window retures screenshot of very front window when the window is covered by it.
import win32gui
import numpy as np
import cv2
import pyautogui
def capture_specific_window(window_name):
hwnd = win32gui.FindWindow(None, window_name)
if not hwnd:
raise Exception('Window not found: ' + window_name)
left, top, right, bot = win32gui.GetClientRect(hwnd)
x, y = win32gui.ClientToScreen(hwnd, (left, top))
return cv2.cvtColor(
np.asarray(
pyautogui.screenshot(
region=(x, y,
*win32gui.ClientToScreen(hwnd, (right - x, bot - y))))), cv2.COLOR_RGB2BGR)