Python 3 中的跨平台截图

Cross-platform screenshot in Python 3

quite a few questions 个类似的,但其中 none 似乎是跨平台的,并且专门针对 Python 3,我正在努力寻找一个可靠的解决方案。

如何在 Python 3 中进行跨平台截图?

可以使用platform.system()找到当前的OS,然后根据操作系统使用不同的解决方案:

import platform
if platform.system()=="Windows":
    ...
elif platform.system()=="Darwin": #Mac
    ...
elif plarform.system()=="Linux":
    ...

我目前的解决方案是使用 PIL 库中的 ImageGrab 函数,如下所示:

from PIL import ImageGrab
image = ImageGrab.grab()

您可以使用 PrtSc 库。

命令:pip3 安装 PrtSc

代码:

import PrtSc.PrtSc as Screen
screen=Screen.PrtSc(True,"file.png")