Rehman Ahmed
Rehman Ahmed

Reputation: 61

Python image recognition with pyautogui

When I try to recognize an image with pyautogui it just says: None

import pyautogui
s = pyautogui.locateOnScreen('Dark.png')
print s

When I ran this code the picture was on my screen but it still failed.

Upvotes: 3

Views: 16705

Answers (3)

Dedektor Dedektoru
Dedektor Dedektoru

Reputation: 59

It's pixel perfect.

It can't find the image if it is not 100% match.

For example, I cropped an area with an Opera extension. Then I ran my script with Firefox, and pyautogui did not recognize it.

  1. Don't let your image get resized or compressed by screen capture software or extensions.
  2. Use the same window/screen (size, resolution) as where you saved your screenshot.

Upvotes: 5

Jonas De Schouwer
Jonas De Schouwer

Reputation: 913

Pyautogui.locateOnScreen has a parameter that specifies the 'confidence' you have in the image you enter.

This way, pyautogui will deal with slight pixel deviations.

For example:

import pyautogui
s = pyautogui.locateOnScreen('Dark.png', confidence=0.9)
print(s)



For more information, see https://buildmedia.readthedocs.org/media/pdf/pyautogui/latest/pyautogui.pdf.

Upvotes: 6

SiHa
SiHa

Reputation: 8411

On my system, I get this if the picture is on a second monitor. If I move it to the main screen, the image is located successfully.

It looks like multiple-monitor functionality is not yet implemented: From http://pyautogui.readthedocs.org/en/latest/roadmap.html

Future features planned (specific versions not planned yet):

  • Find a list of all windows and their captions.
  • Click coordinates relative to a window, instead of the entire screen.
  • Make it easier to work on systems with multiple monitors.
  • ...

Upvotes: 3

Related Questions