Reputation: 1064
I want to click on the center of the image so what can I do I'm tired to click on the centre of the image in pyautogui
For Example for an image we need to write this:
pyautogui.locateOnScreen('image.png')
How Can I locate the center of the image?
Upvotes: 3
Views: 9206
Reputation: 377
x, y = pyautogui.locateCenterOnScreen('neverbtn.png')
pyautogui.click(x, y)
Upvotes: 0
Reputation: 41
As codelt said, locateCenterOnScreen(image) will return x,y and this is the most efficient way.
For more flexibility, remember that locateOnScreen() will also take the confidence argument. This allows you to be flexible about how certain you want it to be before it returns coordinates. You can then run this through center() which will return the X,y coordinates.
For example, if pyautogui was having difficulty finding the image with high degrees of confidence but could find the image with lower confidence:
locateCenterOnScreen(image)
#returns None
locateOnScreen(image, confidence=0.7)
#returns box coordinates as it found the image at a lower confidence
pyautogui.center(pyautogui.locateOnScreen(image,confidence=0.7))
#returns center coordinates, exactly the same as locateCenterOnScreen
Upvotes: 1
Reputation: 3618
You can directly find the center of the image using the built in locateCenterOnScreen method.
locateCenterOnScreen(image, grayscale=False)
Returns (x, y)
coordinates of the center of the first found instance of the image on the screen. Raises ImageNotFoundException
if not found on the screen.
Upvotes: 3