微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python-如何识别图像并单击它们

如何解决python-如何识别图像并单击它们

我想创建一个脚本,该脚本根据要求单击图像,它需要经过图像列表。因此,例如,如果程序要求用户单击绿色圆圈:

question_list = greencircle,redcircle,bluesquare,redtriangle

if(greencircle == greencircle.png){
    pyautogui.click(greencircle.png)
}

有人可以帮忙吗?

解决方法

PyAutoGUI具有内置的function,称为locateOnScreen(),如果可以在当前屏幕上找到图像,则返回图像中心的x,y坐标(需要截屏然后进行分析) )。

图片必须完全匹配 才能起作用;也就是说,如果您想点击button.png,则按钮图片必须与窗口中的按钮具有相同的尺寸/分辨率,程序才能识别它。实现此目的的一种方法是截取屏幕截图,将其打开,然后仅剪切要按下的按钮(否则,您可以让PyAutoGUI为您完成此操作,如后面的示例所示)。

import pyautogui

question_list = ['greencircle','redcircle','bluesquare','redtriangle']

user_input = input('Where should I click? ')

while user_input not in question_list:
    print('Incorrect input,available options: greencircle,redcircle,bluesquare,redtriangle')
    user_input = input('Where should I click?')

location = pyautogui.locateOnScreen(user_input + '.png')
pyautogui.click(location)

上面的示例要求您的目录中已经有greencircle.png和所有其他.png

PyAutoGUI也可以拍摄screenshots,并且您可以指定要拍摄画面的屏幕区域pyautogui.screenshot(region=(0,0))前两个值是您要拍摄的区域左上角的x,y坐标选择,第三个是向右(x)的距离,第四个是向下(y)的距离。

以下示例对Windows 10徽标进行了截屏,将其保存到文件中,然后使用指定的.png文件单击徽标

import pyautogui

pyautogui.screenshot('win10_logo.png',region=(0,1041,50,39))
location = pyautogui.locateOnScreen('win10_logo.png')
pyautogui.click(location)

您也不必将屏幕截图保存到文件中,只需将其保存为变量

import pyautogui

win10 = pyautogui.screenshot(region=(0,39))
location = pyautogui.locateOnScreen(win10)
pyautogui.click(location)

要让程序检测用户是否单击了某个区域(例如Windows 10徽标),将需要另一个pynput之类的库。

from pynput.mouse import Listener    

def on_click(x,y,button,pressed):
    if 0 < x < 50 and 1080 > y > 1041 and str(button) == 'Button.left' and pressed:
        print('You clicked on Windows 10 Logo')
        return False    # get rid of return statement if you want a continuous loop

with Listener(on_click=on_click) as listener:
    listener.join()

将所有内容放在一起

import pyautogui
from pynput.mouse import Listener

win10 = pyautogui.screenshot(region=(0,39))
location = pyautogui.locateOnScreen(win10)

# location[0] is the top left x coord
# location[1] is the top left y coord
# location[2] is the distance from left x coord to right x coord
# location[3] is the distance from top y coord to bottom y coord

x_boundary_left = location[0]
y_boundary_top = location[1]
x_boundary_right = location[0] + location[2]
y_boundary_bottom = location[1] + location[3]


def on_click(x,pressed):
    if x_boundary_left < x < x_boundary_right and y_boundary_bottom > y > y_boundary_top and str(button) == 'Button.left' and pressed:
        print('You clicked on Windows 10 Logo')
        return False    # get rid of return statement if you want a continuous loop


with Listener(on_click=on_click) as listener:
    listener.join()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。