I have a simple code here:
JavaScript
x
4
1
import mss
2
with mss.mss() as sct:
3
filename = sct.shot(output="result.png")
4
result.png
But I want to take a part of screen like this:
Thanks for help!
Advertisement
Answer
As explained on https://python-mss.readthedocs.io/examples.html, something like this should work:
JavaScript
1
12
12
1
with mss.mss() as sct:
2
# The screen part to capture
3
monitor = {"top": 160, "left": 160, "width": 160, "height": 135}
4
output = "sct-{top}x{left}_{width}x{height}.png".format(**monitor)
5
6
# Grab the data
7
sct_img = sct.grab(monitor)
8
9
# Save to the picture file
10
mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
11
print(output)
12
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.