pyautogui.locateOnScreen() Returns... Nothing? - python

I'm trying to use PyAutoGui's image recognition functions. (OS X)
Needless to say, I'm running into some slight issues that I can't seem to solve myself no matter where I look or what I do. I'm attempting to have PyAutoGui click on the Chrome shortcut based off a .png screenshot saved to my desktop.
Here's my code in terminal:
>>>import pyautogui
>>>chrome = pyautogui.locateOnScreen('/Users/ianscalzo/Desktop/chrome.png")
>>>
I get no backfire on my filepath, but it causes my shell/terminal to return nothing but go to a new line. (As shown in the code example above - Just causes terminal to go to a blank ">>>")
I don't really understand why it doesn't do anything but go to a new line, so any insight would be greatly appreciated.
Thank you so much!

After struggling with this forever also, finally figured out that you either use command line to take the screenshot or using the screenshot button with windows key. It doesn't work with the snipping tool.
so try:
image = pyautogui.screenshot()
image.save('testing.png')
Go and crop testing.png as small as possible so that locateOnScreen works faster. Then go back to the terminal and type:
pyautogui.locateOnScreen('testing.png')

Related

pyautogui and pywinauto combine to click application in background

what i am trying to do:
i trying to use the code below to locate and give me a X and Y position of the image
pyautogui.locateCenterOnScreen('accept.png')
after locating the image, i am trying to use pywinauto* to click on the image location in the background.
form.click(button='left', pressed='', coords=(pyautogui.locateCenterOnScreen('accept.png')), double=False, absolute=False)
problem:
nothing seems to happen.. i dont understand why. i checked on the pywinauto* and pyautogui "cheat sheets" it seems okay
someone please enlighten me
The correct spelling is "pywinauto". Method .click_input(...) with the same params is what you need as it moves the cursor and performs realistic click.
Method .click(...) silently sends window messages like WM_LBUTTONDOWN and WM_LBUTTONUP which may not work for some UI elements if they don't handle it.

Colab - Box for input() in python code too long so I need to scroll back to read the question

I plan to use colab notebooks to teach my pupils (12-13 year olds) Python. We will start with using input() and display with print() to ask simple questions and display the answer.
One problem I have come across is when I use input() in a code cell with an input string a really long input box is produced (in the output area) to get the input value. It is so long that the window scrolls to the end of this empty input box, and you have to scroll back to read the question, even when the window is at full screen width . This is really strange as the input box is completely empty does not need to to be bigger than the window or frame.
Are there settings I can change to prevent this or can I do something with the css to reduce the size of this input box?
This may seem a trivial problem, but little things like this can be distracting and add to the frustration of learning the language so I would like to prevent it if I can.
I have attached a picture that shows the problem.
picture that shows the problem.
I looked at the problem you are trying to solve. Colab fits inside my window perfectly when I size the window to full screen. I think this might be a problem with your browser. Are you using the latest version of Firefox or Chrome?
I just encountered the same problem. For me, it was because I was plotting a graph using the matplotlib.pyplot library before prompting for the input. After I removed that, it worked just fine.

Image Recognition - pyautogui

I open up Calculator from window. I use the snipping tool to copy an image of the number 7 button. I paste the image into the paint software and save it as a png file and save it in a directory on my desktop.
I open up the calculator, use this code to locate where the image is on the screen. However the code return a blank space when normally it should return the position of the image on the screen. The first time I ran it, it gave me a coordination but the second time, it just shows me a blank space and I have been trying to figure out why. I kept doing it over and over, re-copied, re-saved the image and rerun the code and it's still the same result, blank. Was wondering what could be the reason.
>>> import pyautogui
>>> pyautogui.locateOnScreen('C:\\Users\\js\\Desktop\\jsPython\\seven2.png')
Maybe you should check your path string.For example, this code runs fine:
import pyautogui
print(pyautogui.locateOnScreen("C:\Python27\source\pyautogui\images\startIcon.png"))
I think you've made a typo in your path string.
Even better solution is to use absolute path.For example :
import pyautogui,os
print(pyautogui.locateOnScreen(os.path.abspath("images\startIcon.png")))

pyautogui.locateCenterOnScreen() returns None instead of coordinates

import pyautogui
print (pyautogui.locateCenterOnScreen("C:\Users\Venkatesh_J\PycharmProjects\mouse_event\mouse_event.png"))
Instead of returning coordinates, it returns None.
My problem is Solved when I took screenshot by pyautogui inbuilt function rather than taking WIN+Printscr because if we took screenshot by WIN+Printscr then pixel density and other image related data may be different in comparison to pyautogui inbuilt function.
Maybe this thing worked for you, for me it worked.
For Ex - wifi.png so first I took full screenshot and I cropped it from that full image then I put this in my code shown below
import pyautogui
print(pyautogui.locateCenterOnScreen('wifi.png'))
Seems like it couldn't find anything matching your image on the screen.
locateCenterOnScreen(image, grayscale=False) - Returns (x, y) coordinates of the center of the first found instance of the image on the screen. Returns None if not found on the screen.
The initial problem is quite simple - the library does not find the image passed represented on the screen and therefore returns None rather than the co-ordinates as it says it will in the docs.
However, there is a possible misunderstanding here, in particular from a user who posted a bounty on the question and posed a similar question here.. A comment was made
"The pictures are on my desktop"
When you use this function, you pass in a filename as a string. The library then loads the image file and looks for the picture on screen (not the filename). pyautogui.locatecentreonscreen() will look for the actual image if it is visible on the screen. It does not look for files on the desktop, or file icons with the same name as the image passed to it.
Example
Say you have a file with the name flower.jpg containing the following image, saved on your desktop.
With no other windows open, run:
coords = pyautogui.locateCenterOnScreen('C:\\Richard\\Users\\flower.jpg')
print(coords)
The result is None
This is because that image is not displayed on my screen even though an icon is on the desktop, with the name flower.jpg. This is true even if that icon is a small scale version of the flower.
However, if I leave the image visible (as I'm preparing this post) and do the same thing, I get co-ordinates - e.g.:
As you see - because the actual image is on the screen, the library finds it, with co-ordinates 524,621
In summary if the library doesn't find the image displayed to the user on the screen, it will return None. Note the image has to be visible to the user at the point at which the code is running. It won't find the icon on your desktop, or similar, or the image in a window that is "hidden" behind another. Is that what you're trying to do?
Are you sure that the image is of the same size as of the icon?
If not pyautogui.locateCenterOnScreen() will raise TypeError: 'NoneType' object is not iterable
Also make sure that the full icon is visible and looks the same as the image:"C:\Users\Venkatesh_J\PycharmProjects\mouse_event\mouse_event.png"
Hope the problem is solved!
Building off of what Don Kirby said, no matching image was found on the screen. You could open the image in, for example, Windows Photo Gallery, (or Tk) and then pyautogui would find it.
Good explanation, is there any library that work better than pyautogui? I mean it wants excatly the same picture on the screen. We need similar sometimes. – GLHF May 11 '16 at 15:45
Try using this code line:
pyautogui.locateCenterOnScreen("yourscreenshot.PNG", confidence=0.9)
I believe confidence range from 0.1-0.9.
Unless you have several pictures looking almost alike, this might solve the exception.
If that doesn't work try making a second screenshot with more/less of the original image and write this code:
try:
pyautogui.locateCenterOnScreen("yourscreenshot.PNG", confidence=0.9)
except TypeError:
pyautogui.locateCenterOnScreen("yourscreenshot2.PNG", confidence=0.9)
This will give it a second try with a slightly different picture, and hopefully not return a TypeError.
If you can't use pyautogui.locateCenterOnScreen() because of image problem , try using the snipping tool (if you are on Windows) to take screenshots.It works.
Also make sure that you have downloaded the "Pillow" module
Try this :
pip install opencv-contrib-python
It confused me a lot that I ran the same code:
coords =pyautogui.locateCenterOnScreen('C:\\test.jpg')
in two different virtual environment( X and Y, almost same) returned None and Point(x=1543, y=461).
I read Aleks's answer and guess it use the parameter confidence implicitly when opencv-contrib-python in current environment(which Y had but X hadn't).
I didn't dig in but just installed opencv-contrib-python in virtual environment X and solved my problem.

Taking screenshot from a python script by selecting the area

I saw that post (that is really helpful : Take a screenshot via a python script. [Linux]) about taking a screenshot from python. It works well but I'd like to have the same behavior as gnome-screenshot : having the possibility to choose between :
Capturing the whole desktop
Capturing an active window
Capturing an area
Is there a way to do this in python, or, eventually, to use the gnome-screenshot application to do it, and then getting the file ?
I tried to find the perfect command line for gnome-screenshot to be launched without asking where to save the screenshot after by giving the path at the call, but I can't find it.
Thanks for your help!
I have a wrapper project (pyscreenshot) for scrot, imagemagick, pyqt, wx and pygtk.
If you have one of them, you can use it.
Capturing an active window is missing.
Install:
easy_install pyscreenshot
Example:
import pyscreenshot as ImageGrab
# fullscreen
im=ImageGrab.grab()
im.show()
# part of the screen
im=ImageGrab.grab(bbox=(10,10,500,500))
im.show()
# to file
ImageGrab.grab_to_file('im.png')
If you are not limited to using using gnome-screenshot specifically, ImageMagick's import command can save directly to file without an interactive prompt.
See here for details: http://www.imagemagick.org/script/import.php
In addition to the command line interface, there is also a Python API.

Categories

Resources