Python: 'pyautogui' has no attribute 'screenshot' (Windows)

瘦欲@ 提交于 2019-12-12 22:34:34

问题


I am trying to take a screenshot with the pyautogui module, but keep getting this error

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

Is there something I'm missing? The chapter in Automate the Boring Stuff with Python said that as I'm on Windows, I shouldn't need to download anything other than pyautogui itself for this to work. Anyone know why this might happen? Thanks in advance.

EDIT: I am using Anaconda so I already have Pillow.


回答1:


On Linux, you must run sudo apt-get install scrot to use the screenshot features.




回答2:


It looks like PyAutoGUI is simply borrowing ImageGrab from PIL/Pillow which you can see by looking in pyautogui inside of screeenshotUtil.py

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

and further down

# 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.

I had the same trouble as OP, but after realizing the above I used ImageGrab directly. Drawing inspiration from the comment section here an answer to OP on windows having pillow installed would look like this:

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))

I have tried. It's seem to different from the document. to hope this is helpful. ­­­­­­­­­­­­­­­­­­­­­­­­­­




回答4:


Before useing "image = pyautogui.screenshot()", you had to type "import pyautogui" before



来源:https://stackoverflow.com/questions/37232282/python-pyautogui-has-no-attribute-screenshot-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!