Python MSS Screen Capture target specific windows

Viewed 1993

Is there any way to screencapture specific windows from windows handle? Even the windows is in the background it still will capture it. currently i only have this code for record the fullscreen desktop not in the specific windows.


def window_capture():
    # Define your Monitor
    x = 0
    y = 0
    w = 1920
    h = 1080
    
    hwnd = None

    with mss.mss() as sct:
        # Part of the screen to capture
        monitor = {"left": x, "top": y, "width": w, "height": h}
        img = sct.grab(monitor)
        img = np.array(img)
    return img



while "Screen capturing":
    # Define Time
    last_time = time()

    # Get the img
    screenshot = window_capture()

    # Display the picture
    cv.imshow('Computer Vision', screenshot)

    print(f'FPS {(1 / (time() - last_time))}')

    # Press "q" to quit
    if cv.waitKey(1) == ord("q"):
        cv.destroyAllWindows()
        break
1 Answers
Related