LocateCenterOnScreen does not work on PyAutoGui

Summary

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.

Execution environment

OS : Ubuntu 16.04 LTS Shell : Zsh Python : anaconda3-4.1.1 on pyenv

What are you angry about?

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.

Investigation

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.

Lesson

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

LocateCenterOnScreen does not work on PyAutoGui
Virtualenv does not work on Python3.5 (Windows)
Jinja2 2.9.6 does not work on Lambda Python 3 series
When the program pip installed on Mac / Marvericks does not work
Path problem does not occur on debian-linux
[VScode] autopep8 format does not work [Python]
gqlgen command does not work with go gqlgen
Notes on what to do when matplotlib scatter () / scatter3d () does not work
After installing php7.2, the php command does not work
When wildcard specification does not work with pylint
jupyter notebook does not start on mac fish
I managed to solve the situation where Python does not work on Mac
What to do when python3 type venv does not work well on Raspberry Pi
Right click event acquisition does not work with opencv-python
Clicking on name attribute in Selenium does not submit
[Pyhton] I want to solve the problem that tkinter does not work on MacOS11
Command when ACPI shutdown does not work in VirtualBox
Parallel processing of Python joblib does not work in uWSGI environment. How to process in parallel on uWSGI?
Day 65 (Solution) Jupyter notebook does not work with Kernel Not Conected.
GrabBox didn't work on Sierra
Python version does not switch
Fcitx doesn't work on Flatpak
distutils log does not appear
Solution when background-cover of Linux version VS Code does not work
The story of the release work of the application that Google does not tell
Arrow keys do not work in zsh + python shell on mac