【问题标题】:How to take a screenshot in Python?如何在 Python 中截取屏幕截图?
【发布时间】:2019-10-24 18:59:43
【问题描述】:

我正在尝试使用可在 Windows 和 Linux 上运行的 python 截取屏幕截图。我读过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,但似乎没有找到具有该名称的软件包。

我不想使用模仿键盘输入的库,因为脚本会在玩游戏时在后台运行以监控统计信息。反作弊可能会被触发。

【问题讨论】:

  • buffer 是 Python 2 中存在但在 Python 3 中不再存在的内置类型的名称。我猜 pyscreenshot 的维护者在移植它时错过了一个地方 :-)
  • 噢噢噢噢噢!这完全有道理。谢谢你启发我。
  • 有趣的是,the most recent version of pyscreenshot's wxscreen.py确实在使用 buffer 之前检查您是否使用 Python 2。也许您以某种方式使用了旧版本的软件包?
  • 看起来这个问题是noticed and fixed back in June,但由于 PyPi 发行版自 2018 年以来尚未更新,因此该修复程序尚未公开访问。如果你真的很想使用这个库,你可以直接从 github 获取它,而不是使用 pip。
  • @Kevin 或者只是从 github 进行 pip-install,请参阅 my answer。好奇:你是如何注意到 PyPi 自 2018 年以来没有更新的?我在release-history看到了3.0 since Apr 18, 2021

标签: python screenshot


【解决方案1】:

您可以使用pyautogui 喜欢:

import pyautogui

myScreenshot = pyautogui.screenshot()

【讨论】:

  • 据我了解,这个库模仿键盘输入?如果我在运行游戏时运行此脚本,我认为它可能会触发反作弊系统。 (我会在问题中澄清)
  • 啊,好吧,buffer() 函数只在 Python 2 中,Python 3 中有 memoryview()。也许尝试使用 Python 2 运行您的代码
【解决方案2】:
  • 您可以使用hek library 轻松截屏
import hek

hek.screen.screenshot(filename="test.png")

【讨论】:

    【解决方案3】:

    pip install git+

    如果 PyPi 不是你喜欢的最新版本,并且有一个你想使用的 git-repo,那么从 git-repo 进行 pip-install。

    例如,as commented by Kevin,因为 GitHub 存储库 ponty/pyscreenshot 包含在 buffer 和 bytes since commit e547f795920d0e4ed5dedb818c2a3a8d7e00a7e5 之间切换所需的平台,从那里 pip-install 软件包:

    $ python3 -m pip install git+https://github.com/ponty/pyscreenshot.git@e547f795920d0e4ed5dedb818c2a3a8d7e00a7e5
    

    或者使用pip3 install git+等等。

    另见:

    【讨论】:

      【解决方案4】:

      另一个真正快速的方法是MSS 模块。它与其他解决方案的不同之处在于它仅使用ctypes 标准模块,因此不需要大的依赖关系。它独立于操作系统,使用起来很容易:

      from mss import mss
      
      with mss() as sct:
          sct.shot()
      

      只需找到包含第一台显示器屏幕截图的screenshot.png 文件。有很多可能的自定义,你可以玩ScreenShot对象和OpenCV/Numpy/PIL/等。

      【讨论】:

      • 好推,Tiger ?️ 没有作者隶属关系或免责声明?!也许我们可以使用 PyScreenshot 的 MSS-wrapper 来两全其美?️
      猜你喜欢
      • 1970-01-01
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-13
      • 1970-01-01
      • 2014-09-26
      相关资源
      最近更新 更多