Skip to content
Advertisement

How to take a screenshot from part of screen with mss python?

I have a simple code here:

import mss
with mss.mss() as sct:
    filename = sct.shot(output="result.png")

result.png

enter image description here

But I want to take a part of screen like this:

enter image description here

Thanks for help!

Advertisement

Answer

As explained on https://python-mss.readthedocs.io/examples.html, something like this should work:

with mss.mss() as sct:
    # The screen part to capture
    monitor = {"top": 160, "left": 160, "width": 160, "height": 135}
    output = "sct-{top}x{left}_{width}x{height}.png".format(**monitor)

    # Grab the data
    sct_img = sct.grab(monitor)

    # Save to the picture file
    mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
    print(output)

This is just the example given on the website. You can adjust the part of the screen that you are taking a screenshot of by modifying the monitor dictionary. As an example you could change it from {"top": 160, "left": 160, "width": 160, "height": 135} to {"top": 10, "left": 14, "width": 13, "height": 105}. You will have to modify it to capture the part of the screen that you want.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement