Search code examples
python-3.xmacoscrashscreenshotpyautogui

How to fix Python 3 PyAutoGUI screenshot error? (macOS)


I keep getting an error with any of PyAutoGUI's screenshot taking functions such as:

pyautogui.locateOnScreen('button.png')
pyautogui.pixelMatchesColor(x, y, (r, g, b))
im = pyautogui.screenshot()

The error I get is:

screencapture: cannot write file to intended destination, .screenshot2018-1009_16-43-26-003190.png
Traceback (most recent call last):
  File "~/program.py", line 111, in <module>
    pyautogui.locateOnScreen('/images/play!.png')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyscreeze/__init__.py", line 265, in locateOnScreen
    screenshotIm = screenshot(region=None) # the locateAll() function must handle cropping to return accurate coordinates, so don't pass a region here.
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyscreeze/__init__.py", line 331, in _screenshot_osx
    im = Image.open(tmpFilename)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PIL/Image.py", line 2609, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: '.screenshot2018-1009_16-43-26-003190.png'

I don't tell it to or want it to save the new screenshotted image to any directory (and it shouldn't). With the pyautogui.screenshot() function I could manually save it to a real directory in my project, but I don't have an option to do that with the other methods. Any idea on how to fix this?

What I've tried:

  • I looked at all the documentation I could find online of pyautogui screenshots
  • Restarting computer
  • Downgrading versions for Pillow and pyscreeze

EDIT: I tried it on another mac and got the same error. Tried it on windows bootcamp (windows on my mac) and it works fine.


Solution

  • possible, very hack-ish fix - I don't actually like this answer but it was a quick and easy fix (done on OSX with Mojave):

    PLEASE NOTE: modifying the source code of libraries you don't understand is usually a bad idea, so do so at your own risk! This worked for me, your milage may vary.

    Go to your file (your file path may be different, I just copied this from your error):

    /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyscreeze/__init__.py
    

    find the line under the function "_screenshot_osx" that looks like

    tmpFilename = '.screenshot%s.png' % (datetime.datetime.now().strftime('%Y-%m%d_%H-%M-%S-%f'))
    

    copy it and then comment it out, paste the copied line directly below the commented out original and modify to something like this:

    tmpFilename = r'<your preferred screenshot folder here>/screenshot%s.png' % (datetime.datetime.now().strftime('%Y-%m%d_%H-%M-%S-%f'))
    

    save the changes, and see if it works.

    Also note: pyautogui.locateOnScreen can be a bit finicky so even if this removes your error you still might not get the coordinates you want (might return none). That might be related to a different issue. To test that part I do this:

    import pyautogui
    pyautogui.screenshot('testFull.png')
    placePos = pyautogui.locateOnScreen('testFull.png')
    print(placePos)
    

    even the cursor blinking can mess this up though, and osx has translucent user interfaces so it's kind of annoying to test this perfectly without careful image curation.