如何在Python中截图?
How to take a screenshot in Python?
我正在尝试使用 python 截取屏幕截图,该屏幕截图适用于 Windows 和 Linux。我读过 pyscreenshot
可以胜任这项工作。但是我有一个错误,文档似乎没有指定任何依赖项。
import pyscreenshot as ImageGrab
im = ImageGrab.grab()
im.show()
回溯:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/pyscreenshot/procutil.py", line 15, in _wrapper
r = target(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/pyscreenshot/__init__.py", line 33, in _grab_simple
return backend_obj.grab(bbox)
File "/usr/local/lib/python3.6/dist-packages/pyscreenshot/plugins/wxscreen.py", line 39, in grab
im.frombytes(buffer(myWxImage.GetData()))
NameError: name 'buffer' is not defined
Traceback (most recent call last):
File "ambi.py", line 10, in <module>
im = ImageGrab.grab()
File "/usr/local/lib/python3.6/dist-packages/pyscreenshot/__init__.py", line 67, in grab
to_file=False, childprocess=childprocess, backend=backend, bbox=bbox)
File "/usr/local/lib/python3.6/dist-packages/pyscreenshot/__init__.py", line 46, in _grab
_grab_simple, imcodec.codec, to_file, backend, bbox, filename)
File "/usr/local/lib/python3.6/dist-packages/pyscreenshot/procutil.py", line 37, in run_in_childprocess
raise e
NameError: name 'buffer' is not defined
我用sudo pip3 install pyscreenshot
安装的
我尝试安装 wxscreen
,但它似乎没有找到具有该名称的软件包。
我不想使用模仿键盘输入的库,因为在玩游戏时脚本会 运行 在后台监控统计数据。可能会触发反作弊。
您可以使用 pyautogui
如:
import pyautogui
myScreenshot = pyautogui.screenshot()
另一种非常快速的方法是 MSS 模块。它与其他解决方案的不同之处在于它仅使用 ctypes
标准模块,因此不需要大的依赖性。它是 OS 独立的并且使用起来很容易:
from mss import mss
with mss() as sct:
sct.shot()
然后找到包含第一个监视器屏幕截图的 screenshot.png
文件。有很多可能的定制,你可以玩 ScreenShot
对象和 OpenCV/Numpy/PIL/etc..
- 您可以使用 hek 库轻松截取屏幕截图
import hek
hek.screen.screenshot(filename="test.png")
pip install git+
如果 PyPi 不像您喜欢的那样 up-to-date 并且有一个 git-repo 您想要使用,那么 pip-install 来自 git-repo。
例如, because GitHub repo ponty/pyscreenshot contains the needed platform-switch between buffer
and bytes
since commit e547f795920d0e4ed5dedb818c2a3a8d7e00a7e5
,pip-install 那里的包裹:
$ python3 -m pip install git+https://github.com/ponty/pyscreenshot.git@e547f795920d0e4ed5dedb818c2a3a8d7e00a7e5
或使用pip3 install git+
等。
另请参阅:
- pip: pulling updates from remote git repository
- pip install from git repo branch
- 亚当·约翰逊:‘pip install’ From a Git Repository
我正在尝试使用 python 截取屏幕截图,该屏幕截图适用于 Windows 和 Linux。我读过 pyscreenshot
可以胜任这项工作。但是我有一个错误,文档似乎没有指定任何依赖项。
import pyscreenshot as ImageGrab
im = ImageGrab.grab()
im.show()
回溯:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/pyscreenshot/procutil.py", line 15, in _wrapper
r = target(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/pyscreenshot/__init__.py", line 33, in _grab_simple
return backend_obj.grab(bbox)
File "/usr/local/lib/python3.6/dist-packages/pyscreenshot/plugins/wxscreen.py", line 39, in grab
im.frombytes(buffer(myWxImage.GetData()))
NameError: name 'buffer' is not defined
Traceback (most recent call last):
File "ambi.py", line 10, in <module>
im = ImageGrab.grab()
File "/usr/local/lib/python3.6/dist-packages/pyscreenshot/__init__.py", line 67, in grab
to_file=False, childprocess=childprocess, backend=backend, bbox=bbox)
File "/usr/local/lib/python3.6/dist-packages/pyscreenshot/__init__.py", line 46, in _grab
_grab_simple, imcodec.codec, to_file, backend, bbox, filename)
File "/usr/local/lib/python3.6/dist-packages/pyscreenshot/procutil.py", line 37, in run_in_childprocess
raise e
NameError: name 'buffer' is not defined
我用sudo pip3 install pyscreenshot
我尝试安装 wxscreen
,但它似乎没有找到具有该名称的软件包。
我不想使用模仿键盘输入的库,因为在玩游戏时脚本会 运行 在后台监控统计数据。可能会触发反作弊。
您可以使用 pyautogui
如:
import pyautogui
myScreenshot = pyautogui.screenshot()
另一种非常快速的方法是 MSS 模块。它与其他解决方案的不同之处在于它仅使用 ctypes
标准模块,因此不需要大的依赖性。它是 OS 独立的并且使用起来很容易:
from mss import mss
with mss() as sct:
sct.shot()
然后找到包含第一个监视器屏幕截图的 screenshot.png
文件。有很多可能的定制,你可以玩 ScreenShot
对象和 OpenCV/Numpy/PIL/etc..
- 您可以使用 hek 库轻松截取屏幕截图
import hek
hek.screen.screenshot(filename="test.png")
pip install git+
如果 PyPi 不像您喜欢的那样 up-to-date 并且有一个 git-repo 您想要使用,那么 pip-install 来自 git-repo。
例如,buffer
and bytes
since commit e547f795920d0e4ed5dedb818c2a3a8d7e00a7e5
,pip-install 那里的包裹:
$ python3 -m pip install git+https://github.com/ponty/pyscreenshot.git@e547f795920d0e4ed5dedb818c2a3a8d7e00a7e5
或使用pip3 install git+
等。
另请参阅:
- pip: pulling updates from remote git repository
- pip install from git repo branch
- 亚当·约翰逊:‘pip install’ From a Git Repository