#đź”’ How would i search for an image using python

23 messages · Page 1 of 1 (latest)

warm iceBOT
#

@high sage

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

neon jay
#

hi lol

if you wanna locate an image on screen (or simply check if an image is on screen), you can use the library pyautogui

simply use its function locateCenterOnScreen()

the main parameters are: image path and confidence (1 is pixel perfect, so maybe set it to like 0.8, you can tweak it until it works)

also confidence is an optional parameter, so you’ll have to type confidence= before putting the confidence

the function will return the pyautogui position of the center of the image!

for example:

import pyautogui

#get the center location of an image
pos = pyautogui.locateCenterOnScreen(“imgPath”, confidence=0.8)

also, if it doesn’t detect your image, it will raise an error.

#

yw

#

also you have to pip install the library

#

well, if it’s at 1 (100%), it will only detect the image if it is perfectly clear and if it has the right size, as the confidence would have to be at max

#

so it will search for the image and calculate a confidence to every pixel array it finds, and if the confidence is higher/equal to your requirement it’ll return the location

#

yea, but the accuracy would decrease, as it might detect other things as the image

#

so just tweak it until it only detects the image

limpid eagle
#

i don't think pyautogui works with different sizes or other geometric transformations.

#

what kind of transformations?

#

you could try searching multiple times with scaled versions of the pattern. but i think there should also be better functions in opencv for "scale invariant template matching" or "feature matching".

limpid eagle
#

i think none of these are opencv. opencv is a dependency of these packages. just like you need opencv installed to use the confidence parameter in pyautogui.

#

the package is called opencv-python

unique marten
#

hi

#

found ur post lol

limpid eagle
#

hm.. when you find an image you want to print a number.

#

do you know how to print numbers?

#

yes, you don't even have to compare it with True.

if res:
    print(1)```
#

that depends on the version. as of version 0.9.41, if the locate functions can’t find the provided image, they’ll raise ImageNotFoundException instead of returning None. you have to catch the exception with a try-except:

try:
    code
except ImageNotFoundException:
    pass```
#

with code being your code.

warm iceBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.