asweigart / pyautogui

A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
BSD 3-Clause "New" or "Revised" License
10.17k stars 1.23k forks source link

Image location directory? #208

Open rimmi2002 opened 6 years ago

rimmi2002 commented 6 years ago

I'm trying to use the following line: pyautogui.locateOnScreen('Amazonsearch.png')

but I get the following error: 'Traceback (most recent call last): File "/Volumes/3TB Storage/Downloads/Test Python", line 5, in pyautogui.locateOnScreen('Amazonsearch.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 327, in _screenshot_osx subprocess.call(['screencapture', '-x', tmpFilename]) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 267, in call with Popen(*popenargs, **kwargs) as p: File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 707, in init restore_signals, start_new_session) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1333, in _execute_child raise child_exception_type(errno_num, err_msg) FileNotFoundError: [Errno 2] No such file or directory: 'screencapture' [Finished in 0.5s]'

what is the reason for this error? How can a correct it. Thanks.

glitchassassin commented 6 years ago

It's telling you that it can't find the screencapture command.

You're running in OS X, correct? What happens when you run screencapture in Terminal?

rimmi2002 commented 6 years ago

Screen capture command works fine in terminal. I am using macosx 10.12.6

ottoscholten commented 5 years ago

Did you ever figure out how to fix this?

nandish017 commented 4 years ago

Is there any update on this?

ottoscholten commented 4 years ago

I ended up using the pyautogui.locate() function, that seemed to work for me.

J0han3s commented 2 years ago

I am having this issue when I try to run my code from systemd, else it works perfectly, if i do not.

Can anyone advise what i need to do so that when it runs as systemd it runs the way it should. Thanks

JayRizzo commented 2 years ago

Hi all, I believe the screenshot saves to whatever is the current directory. I don't experience any issues when using it.

As an example: Here is a copy pasta script Try something like this:


# Import the os module & pyautogui
import os
import pyautogui

# 1 Set the file name
filename = 'test2342342.png'

# 2 Print the current working directory
print("Current working directory: {0}".format(os.getcwd()))

# 3 Change the current working directory to home
current_home = os.path.expanduser('~')
os.chdir(current_home)
print("Current working directory: {0}".format(os.getcwd()))

# 4 Take a Screenshot
im1 = pyautogui.screenshot()
im1.save(filename)

# 5 Check current path and if the file exists
current_path = os.path.realpath('.')
path_to_file = os.path.join(current_path, filename)
if exists(path_to_file):
    print('Success Image Found At: {}'.format(path_to_file))
else:
    print('Failed Image Not Found At: {}'.format(path_to_file))

Results

Success Image Found At: /Users/jkirchoff/test2342342.png


# Change the current working directory to temp and check for the file again.
os.chdir('/tmp')

current_path = os.path.realpath('.')
path_to_file = os.path.join(current_path, filename)
if exists(path_to_file):
    print('Success Image Found At: {}'.format(path_to_file))
else:
    print('Failed Image Not Found At: {}'.format(path_to_file))

Results

Failed Image Not Found At: /private/tmp/test2342342.png

Hope it helps! Have a Great Day All!