python-imagesearch icon indicating copy to clipboard operation
python-imagesearch copied to clipboard

imagesearch returns wrong coordinates

Open ricvillagrana opened this issue 5 years ago • 7 comments

I searched for the position of an image, as your example shows, and if it is on the top-left corner the position is ok, but as it gets far from there, it will increase the coordinates even doubling them.

I divided them by 2 and it is close to the actual position but not enough:

from imagesearch import *

pos = imagesearch('notif.jpg')

if pos[0] != -1:
    print("position : ", pos[0], pos[1])
    pyautogui.moveTo(pos[0]/2, pos[1]/2)
    pyautogui.click(button="left")
else:
    print("image not found")

I am using Mac OS, also I use 2 monitors, but the problem is still there if I only use one.

ricvillagrana avatar Aug 06 '19 18:08 ricvillagrana

Hello, I've tested your code and it runs fine on my end (ubuntu 18, double monitors as well). Since you have a mac I assume that the problem might be related to your high resolution. I have never tested it with resolutions higher than 1920×1080.

I've had a guy in the past with a retina display that had errors and here's his comment :

"However I had troubles with retina display and the screenshot function that was returning a double resolution image. Here is the code with simple retina support, tested only on Mac OS : https://github.com/anthonyivol/python-imagesearch/blob/master/imagesearch.py "

drov0 avatar Aug 06 '19 19:08 drov0

I've actually updated the lib with retina support, tell me how that goes for you :)

drov0 avatar Aug 06 '19 19:08 drov0

Thank you so much for your quick response. I rebased master but now the imagesearch does not find the image, it prints image not found.

I also commented the is_retina validation and it seems not to have changes in the result.

ricvillagrana avatar Aug 07 '19 14:08 ricvillagrana

Huh, that's odd, can you save the screencap and see if your searched image is in it ? (uncomment im.save('testarea.png'))

drov0 avatar Aug 08 '19 08:08 drov0

Yes it is. These are the images, the testarea, and the image to search. testarea img

ricvillagrana avatar Aug 08 '19 17:08 ricvillagrana

The size of the logo you're trying to find must match the size of the logo on the screencap. This is not a fancy machine learning image searching that will search things that resemble the input image. What opencv does is basically take the input image and then look for something of the same size and more or less the same colored pixels (this is what the tolerance parameter does btw). and if your input image is bigger than the one that's displayed, then it won't find it.

here's an example of a screencap + logo that works :

image

image

drov0 avatar Aug 08 '19 18:08 drov0

Here is an option: https://www.pyimagesearch.com/2015/01/26/multi-scale-template-matching-using-python-opencv/

But if you can click by a unique value (pixel) then you save a lot of cycles. You could even do a branching pixel check from a color to rule out other colors on the screen which may be the same.

eagleEggs avatar Aug 08 '19 19:08 eagleEggs