我这里有一个简单的代码:
import mss
with mss.mss() as sct:
filename = sct.shot(output="result.png")
Run Code Online (Sandbox Code Playgroud)
结果.png
但我想像这样截取屏幕的一部分:
感谢帮助!
正如https://python-mss.readthedocs.io/examples.html上所解释的,这样的东西应该有效:
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)
Run Code Online (Sandbox Code Playgroud)
这只是网站上给出的示例。您可以通过修改词典来调整要截图的屏幕部分monitor。例如,您可以将其从 更改{"top": 160, "left": 160, "width": 160, "height": 135}为{"top": 10, "left": 14, "width": 13, "height": 105}。您必须修改它才能捕获您想要的屏幕部分。