如何在pyautogui中点击图像中心
How to click on center of image in pyautogui
我想点击图片中心怎么办我在pyautogui中点击图片中心很累
例如,对于图像,我们需要这样写:
pyautogui.locateOnScreen('image.png')
如何定位图像的中心?
您可以使用内置的 locateCenterOnScreen 方法直接找到图像的中心。
locateCenterOnScreen(image, grayscale=False)
Returns (x, y)
在屏幕上找到的第一个图像实例的中心坐标。如果在屏幕上找不到,则引发 ImageNotFoundException
。
正如 codelt 所说,locateCenterOnScreen(image) 将 return x,y,这是最有效的方法。
为了获得更大的灵活性,请记住 locateOnScreen() 还将采用 confidence 参数。这使您可以灵活地确定它在returns 坐标之前的位置。然后你可以 运行 通过 center() 这将 return X,y 坐标。
例如,如果 pyautogui 很难找到置信度高的图像,但可以找到置信度较低的图像:
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
x, y = pyautogui.locateCenterOnScreen('neverbtn.png')
pyautogui.click(x, y)
我想点击图片中心怎么办我在pyautogui中点击图片中心很累
例如,对于图像,我们需要这样写:
pyautogui.locateOnScreen('image.png')
如何定位图像的中心?
您可以使用内置的 locateCenterOnScreen 方法直接找到图像的中心。
locateCenterOnScreen(image, grayscale=False)
Returns (x, y)
在屏幕上找到的第一个图像实例的中心坐标。如果在屏幕上找不到,则引发 ImageNotFoundException
。
正如 codelt 所说,locateCenterOnScreen(image) 将 return x,y,这是最有效的方法。
为了获得更大的灵活性,请记住 locateOnScreen() 还将采用 confidence 参数。这使您可以灵活地确定它在returns 坐标之前的位置。然后你可以 运行 通过 center() 这将 return X,y 坐标。
例如,如果 pyautogui 很难找到置信度高的图像,但可以找到置信度较低的图像:
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
x, y = pyautogui.locateCenterOnScreen('neverbtn.png')
pyautogui.click(x, y)