Find real-time coordinates python mss and show it

108 views Asked by At

I have some code below. I want find some process and get real-time screenshot of that window. I need to get right coordinates.

I have next construction:

import win32gui
import numpy as np
import cv2
from mss import mss
import psutil
from pywinauto import Application


#   scan active process
process_name = "testapp.exe"
pid = None

for proc in psutil.process_iter():
    if process_name in proc.name():
        pid = proc.pid
        print('testapprun')
        p = psutil.Process(pid)
        p.nice(256)
        print(p)
        app = Application().connect(process=pid)
        app.top_window().set_focus()
        break
    else:
        print('Now check PID = ', proc.pid)

x, y, w, h = 0, 0, 0, 0

def callback():
    rect = win32gui.GetWindowRect(win32gui.FindWindow(None, "testapp"))
    global x, y, w, h
    x = rect[0]
    y = rect[1]
    w = rect[2] - x
    h = rect[3] - y
    print("\tLocation: (%d, %d)" % (x, y))
    print("\t    Size: (%d, %d)" % (w, h))
    bounding_box = {'top': x, 'left': y, 'width': 800, 'height': 600}
    global sct_img
    sct_img = sct.grab(bounding_box)

sct = mss()

while True:
    callback()
    cv2.imshow('screen', np.array(sct_img))
    if (cv2.waitKey(1) & 0xFF) == ord('q'):
        cv2.destroyAllWindows()
        break

if I try to move the target window, my cv2.imshow shows the wrong coordinates. But I think I'm doing the right code. What I want is this: when I move the "testapp" window, cv2.imshow stays at those coordinates, but shows (a quick screenshot of course) the correct window (testapp).

I try: change win32gui.GetWindowRect to win32gui.GetClientRect or win32gui.GetWindowPostition

But im newbee in python and have only small pet-project)

0

There are 0 answers