Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python MSS Screen Capture target specific windows

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
like image 570
whyeyes Avatar asked Jul 12 '26 19:07

whyeyes


1 Answers

Hi I was searching for same answer as you, it looks like mss cant do that.

Possible way how to do that is with win32api.

Check this: Fastest way to take a screenshot with python on windows

like image 61
glorindel Avatar answered Jul 14 '26 07:07

glorindel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!