asweigart / pyautogui

A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
BSD 3-Clause "New" or "Revised" License
10.24k stars 1.24k forks source link

feature request: tolerance for locate on screen #150

Closed jeremygray closed 4 years ago

jeremygray commented 7 years ago

(Picking up from the tail end of discussion on #4)

A tolerance for locating on screen would be of interest to several people.

Possible easy solution: expose the "confidence" parameter, allowing people to locate with reduced confidence ( = more tolerance for mismatches)

Ralle8370 commented 6 years ago

Any update on this? :)

bjanzen commented 6 years ago

Add confidence value, and confirm you can use cv2:
pyautogui.locateCenterOnScreen('foo.png', confidence=0.999) lower default values. I just tried it with the image not on screen, it found something similar at confidence=0.5.

alphaCTzo7G commented 6 years ago

This works great.. perhaps this should be documented in Pyautogui..

jeremygray commented 5 years ago

I think that if this were documented, we could close this issue. If you have opencv installed, the code works, passing confidence=.9 (or whatever).

ghost commented 5 years ago

I have installed opencv via: sudo python3 -m pip install opencv-contrib-python which gave me version: "opencv_contrib_python-4.0.0.21-cp35-cp35m-manylinux1_x86_64" and pyautogui won't seem to recognize this.

If someone could assist me by telling me if a particular version of opencv is required, or if I have done something incorrect you can notice? Thank you.

Additional Notes: Trying to use: if pyautogui.locateCenterOnScreen(myVar, confidence=.9) is not None: but get error: TypeError: _locateAll_python() got an unexpected keyword argument 'confidence' which as I can conclude from reading research means pyautogui is not detecting my opencv installation.

Sharbell commented 5 years ago

You need to install opencv-python pip install opencv-python

kukaiN commented 5 years ago

So i have opencv installed and the locate function is working. but how does the confidence work? does it work for rescaled images? what about color, shape, size, flipped/inverted, etc?

jeremygray commented 5 years ago

confidence is the value that gets used when thresholding the normalized cross-correlation values. It will not handle changes in image size or orientation at all gracefully, although tiny changes could still match. Not sure what happens with color but you might be able to do locate() using grayscale images.

waterfronter commented 4 years ago

I install cv2, so calling pyautogui.locateOnScreen will eventually call _locateAll_opencv in pyscreeze moudle. First,I called pyautogui.locateOnScreen with param confidence=0.8, and found this value doesn't work, still return None. So I realized pyautogui didn't transfer the param confidence to pyscreeze. Therefore, I changed confidence to 0.8 (default value is 0.999) in function _locateAll_opencv directly, and it worked!

Here I suggest pyautogui use confidence param in locate related functions.