Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 629
Rehman Ahmed Avatar asked Sep 11 '26 09:09

Rehman Ahmed


2 Answers

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.

like image 90
Jonas De Schouwer Avatar answered Sep 12 '26 23:09

Jonas De Schouwer


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.
like image 34
Dedektor Dedektoru Avatar answered Sep 12 '26 22:09

Dedektor Dedektoru