Answers for "Python screen capture specific window"

0

python screen capture a window

from win32gui import FindWindow, GetWindowRect
from PIL import ImageGrab
from PIL import Image
import numpy as np
import cv2

while True:
    window_handle = FindWindow(None, "MTGA")
    window_rect = GetWindowRect(window_handle)
    screen = np.array(ImageGrab.grab(bbox=(window_rect)))
    resized = cv2.resize(screen, (1280, 720), interpolation = cv2.INTER_AREA)
    im_rgb = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB)
    cv2.imshow('Python Window', im_rgb)
    if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break
Posted by: Guest on April-28-2021
0

python screenshot specific window

import pyautogui
import win32gui

def screenshot(window_title=None):
    if window_title:
        hwnd = win32gui.FindWindow(None, window_title)
        if hwnd:
            win32gui.SetForegroundWindow(hwnd)
            x, y, x1, y1 = win32gui.GetClientRect(hwnd)
            x, y = win32gui.ClientToScreen(hwnd, (x, y))
            x1, y1 = win32gui.ClientToScreen(hwnd, (x1 - x, y1 - y))
            im = pyautogui.screenshot(region=(x, y, x1, y1))
            return im
        else:
            print('Window not found!')
    else:
        im = pyautogui.screenshot()
        return im


im = screenshot('Calculator')
if im:
    im.show()
Posted by: Guest on March-24-2021

Python Answers by Framework

Browse Popular Code Answers by Language