Python Forum

Full Version: i want to locate an image according to the mouse position
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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')
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.