gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:09:46 26 4
gpt4 key购买 nike

我想制作一个脚本,根据询问的内容点击图像,它需要通过图像列表。例如,如果程序要求用户点击绿色圆圈:

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, 0, 0)) 前两个值是左上角的 x,y 坐标您要选择的区域,第三个是向右多远(x),第四个是向下多远(y)。

以下示例截取 Windows 10 Logo ,将其保存到文件中,然后使用指定的 .png 文件单击 Logo

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, 1041, 50, 39))
location = pyautogui.locateOnScreen(win10)
pyautogui.click(location)

让程序检测用户是否点击了某个区域(比如说,windows 10 Logo )将需要另一个库,如 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, 1041, 50, 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, y, button, 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()

关于python - 如何识别图像并点击它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64790303/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com