Python Forum
i want to locate an image according to the mouse position - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: i want to locate an image according to the mouse position (/thread-32406.html)



i want to locate an image according to the mouse position - rachidel07 - Feb-07-2021

i want to to locate an image on screen by "Pyautogui.locateOnScreen" but i want the region depend to the mouse. for example if the mouse in position x,y so i want to locate on screen from this x,y position .
another example: this is the code for locate-onscreen in a region: region=(100,100,100,100) what i want like this region=(my mouse position, Width , height).
image to explain more :
[Image: 1IgRa.png]
this is my code
import pyautogui
import time

time.sleep(5)

while 1:
    img = pyautogui.locateOnScreen('test.png',confidence=0.8,region=(280,284,261,228))
    if img:

        pyautogui.moveTo(img)
        time.sleep(2)
        pyautogui.moveRel(-196, 5)
       # pyautogui.locateOnScreen(here where i want add the region who depends on the mouse position ) )

    else:
        
        print ('non')



RE: i want to locate an image according to the mouse position - michael1789 - Feb-08-2021

img = pyautogui.locateOnScreen('test.png',confidence=0.8,region=(280,284,261,228))

I think... (280,284,261,228) represents (x, y, height, width) of the image you want. So if you use the x,y of your mouse position in there the image location will be the mouse location.

eg: (your_mouse.x - 50, your_mouse.y - 50, 100, 100) will give you a 100x100 square centered on the mouse.