如何使用mss python截取屏幕的一部分?

Dil*_*ile 7 python screenshot

我这里有一个简单的代码:

import mss
with mss.mss() as sct:
    filename = sct.shot(output="result.png")
Run Code Online (Sandbox Code Playgroud)

结果.png

在此输入图像描述

但我想像这样截取屏幕的一部分:

在此输入图像描述

感谢帮助!

cat*_*rus 6

正如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}。您必须修改它才能捕获您想要的屏幕部分。

  • 文档的这一部分解释了监视器参数(左/上/宽/高):https://python-mss.readthedocs.io/api.html?highlight=monitor%3C#mss.tools.mss.base .MSSBase (3认同)