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

1

7

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!

Paralipomena answered 13/3, 2022 at 1:28 Comment(0)
F
6

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.

Fossick answered 13/3, 2022 at 1:34 Comment(1)
The monitor params (left/top/width/height) are explained in this part of the doc: python-mss.readthedocs.io/…Penalize

© 2022 - 2024 — McMap. All rights reserved.