import pyautogui as pa
position = pa.locateCenterOnScreen("target.png ")
locateOnScreen ("target.png ")
outputs the coordinates of the center of the location that matches target.png on the screen.
Traceback (most recent call last):
File "test.py", line 5, in <module>
position = pa.locateCenterOnScreen("target.png ")
File "/home/USER/.pyenv/versions/anaconda3-4.1.1/lib/python3.5/site-packages/pyscreeze/__init__.py", line 122, in locateCenterOnScreen
return center(locateOnScreen(image, grayscale))
File "/home/USER/.pyenv/versions/anaconda3-4.1.1/lib/python3.5/site-packages/pyscreeze/__init__.py", line 105, in locateOnScreen
screenshotIm = screenshot()
File "/home/USER/.pyenv/versions/anaconda3-4.1.1/lib/python3.5/site-packages/pyscreeze/__init__.py", line 156, in _screenshot_linux
raise NotImplementedError('"scrot" must be installed to use screenshot functions in Linux. Run: sudo apt-get install scrot')
NotImplementedError: "scrot" must be installed to use screenshot functions in Linux. Run: sudo apt-get install scrot
I got angry.
OS : Ubuntu 16.04 LTS Shell : Zsh Python : anaconda3-4.1.1 on pyenv
It says "I don't have scrot
", but I've already installed it (scrot
is a command to take screenshots from the terminal). Apparently the error flies from pyscreeze
instead of ** pyautogui
It seems that it is. ** The method to determine if scrot
of ... / pyscreeze / __ init__.py
is installed may not work properly.
The variable scrotExists
in ... / pyscreeze / __ init__.py
controls the presence or absence of scrot
:
try:
if sys.platform not in ('java', 'darwin', 'win32'):
whichProc = subprocess.Popen(['which', 'scrot'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
scrotExists = whichProc.wait() == 0
except:
# if there is no "which" program to find scrot, then assume there is no scrot.
pass
What makes me angry that "there is no scrot
"is that scrotExists
is False. It seems that subprocess.Popen (). Wait ()
is not working as expected. As a result of moving, shell = True
is suspicious.
Popen
If you specify shell = True
, the shell to be executed on Ubuntu seems to be / bin / sh
. If shell = False
, it will be executed by zsh in your environment:
>>> sp.Popen("echo $0", shell=True).wait()
/bin/sh
0
>>> sp.Popen(["ps"]).wait()
PID TTY TIME CMD
2418 pts/20 00:00:00 python
2871 pts/20 00:00:00 ps
21011 pts/20 00:00:00 zsh
26515 pts/20 00:00:24 emacs
0
The return value of zero is the return code. It ends normally. As you can see, the command format differs depending on the specification of "shell". The former passes string
, the latter passes list
. / bin I wondered if / sh
was bad, but it wasn't. After all, "pass with shell = True
and string
" or "pass with shell = False
and list
" Either one worked. I haven't investigated it properly, but is the specification of subprocess.popen ()
different in the python version, or is it another reason?
I wonder if this will work, but I'm still angry:
Traceback (most recent call last):
File "test.py", line 5, in <module>
position = pa.locateCenterOnScreen("images/target.png ")
File "/home/USER/.pyenv/versions/anaconda3-4.1.1/lib/python3.5/site-packages/pyscreeze/__init__.py", line 120, in locateCenterOnScreen
return center(locateOnScreen(image, grayscale))
File "/home/USER/.pyenv/versions/anaconda3-4.1.1/lib/python3.5/site-packages/pyscreeze/__init__.py", line 106, in locateOnScreen
screenshotIm.fp.close() # Screenshots on Windows won't have an fp since they came from ImageGrab, not a file.
AttributeError: 'NoneType' object has no attribute 'close'
" ScreenshotIm.fp
is a NonType
.". Even if it's an object that needs to be closed, I commented out this part because the destructor would follow it.
It worked for the time being, so when I tried to throw a Pull Request to github, it was already fixed:
def locateAllOnScreen(image, **kwargs):
screenshotIm = screenshot(region=None) # the locateAll() function must handle cropping to return accurate coordinates, so don't pass a region here.
retVal = locateAll(image, screenshotIm, **kwargs)
try:
screenshotIm.fp.close()
except AttributeError:
# Screenshots on Windows won't have an fp since they came from
# ImageGrab, not a file. Screenshots on Linux will have fp set
# to None since the file has been unlinked
pass
return retVal
With polite comments. Looking at the version, github is 0.1.8
. Conda cloud
is 0.1.0
.
Let's use the latest version. I learned a little.
Recommended Posts