best way to locate a color changing button

时间:2018-04-20 01:02:51

标签: python python-3.x opencv pyautogui

The code I currently have is:

import pyautogui

location_button = pyautogui.locateOnScreen('Button.png')
x, y = pyautogui.center(location_button)
pyautogui.click(x, y)

The code tries to find a exact image of Button.png on the screen, get the coordinate, and click the coordinate.

The problem is that the Button changes color gradually, so sometimes location_button returns None. What is the best way to get a positive match all the time even if the button color changes?

Greyscale doesn't because there are particles flying around sometimes get in the Button.

Would binary image work?

Thanks

1 个答案:

答案 0 :(得分:0)

Create image files for all the different button colors, named Button0.png, Button1.png, etc., and search for them in a loop.

for i in range(button_image_count):
    location_button = pyautogui.locateOnScreen('Button%d.png' % 10)
    if location_button:
        break