【问题标题】:Python: 'pyautogui' has no attribute 'screenshot' (Windows)Python:“pyautogui”没有属性“屏幕截图”(Windows)
【发布时间】:2018-01-22 22:06:54
【问题描述】:

我正在尝试使用pyautogui 模块截屏,但一直收到此错误

>>> image = pyautogui.screenshot()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'pyautogui' has no attribute 'screenshot'

我有什么遗漏吗? Automate the Boring Stuff with Python 中的章节说,当我在 Windows 上时,除了 pyautogui 本身之外,我不需要下载任何东西来工作。有谁知道为什么会发生这种情况?提前致谢。

编辑:我正在使用 Anaconda,所以我已经有 Pillow

【问题讨论】:

  • 尝试使用 sudo pip install -U pyautogui 更新 pyautogui
  • 根据the documentation,你也需要枕头。
  • 忽略了:我正在使用 Anaconda,所以已经有了 Pillow。现在将编辑问题以包含该问题。另外,我的 pyautogui 是最新的。
  • 你在某处还有其他名为“pyautogui.py”的脚本吗?喜欢在当前目录中?
  • @kindall 不,我不这么认为。可能有一个隐藏在某个地方,但我没有创建一个,所以我看不出有任何理由应该存在一个。为什么?

标签: python windows screenshot pyautogui


【解决方案1】:

在 Linux 上,您必须运行 sudo apt-get install scrot 才能使用屏幕截图功能。

【讨论】:

    【解决方案2】:

    看起来 PyAutoGUI 只是从 PIL/Pillow 借用 ImageGrab,您可以通过查看 screenshotUtil.py 中的 pyautogui 来看到这一点

    def _screenshot_win32(imageFilename=None):
        im = ImageGrab.grab()
        if imageFilename is not None:
            im.save(imageFilename)
        return im
    

    再往下

    # set the screenshot() function based on the platform running this module
    if sys.platform.startswith('java'):
        raise NotImplementedError('Jython is not yet supported by PyAutoGUI.')
    elif sys.platform == 'darwin':
        screenshot = _screenshot_osx
    elif sys.platform == 'win32':
        screenshot = _screenshot_win32
        from PIL import ImageGrab
    else:
        screenshot = _screenshot_linux
    
    grab = screenshot # for compatibility with Pillow/PIL's ImageGrab module.
    

    我遇到了和 OP 一样的麻烦,但是在意识到以上之后我直接使用了 ImageGrab。从评论部分here 中汲取灵感,在安装了枕头的 Windows 上对 OP 的回答如下所示:

    from PIL import ImageGrab
    import time
    
    time.sleep(5)
    box = (1106,657,1166,679) #Upper left and lower right corners
    ImageGrab.grab(box).save('box.png') #ImageGrab.grab().save('fullscreen.png')
    

    【讨论】:

      【解决方案3】:
      from pyautogui import screenshotUtil
      
      im=screenshotUtil.screenshot()
      
      print im.getpixel((850, 850))
      

      我试过了。它似乎与文档不同。希望这是有帮助的。

      【讨论】:

        【解决方案4】:

        在使用“image = pyautogui.screenshot()”之前,您必须先输入“import pyautogui”

        【讨论】:

          猜你喜欢
          • 2011-06-03
          • 1970-01-01
          • 2021-04-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-03-20
          • 1970-01-01
          • 2020-12-23
          相关资源
          最近更新 更多