Python: 'pyautogui' has no attribute '

2019-07-27 04:43发布

问题:

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