Im got problem with my bot, he must select aplication and on aplication when im do something, then small blue icon appear on the screen, then my bot must click a icon, but there is a problem, hes not clicking on icon and im dont know why. Icon is small.
This is my code:
`import pyautogui
import cv2
import numpy as np
import time
import mss
import pygetwindow as gw
template_path = 'blue_icon.png'
app_title = "XXX"
def find_and_click_blue_icon():
app_window = gw.getWindowsWithTitle(app_title)
if not app_window:
print(f'Nie znaleziono aplikacji o nazwie: {app_title}')
return
app_window = app_window[0]
app_window.activate()
left, top, width, height = app_window.left, app_window.top, app_window.width, app_window.height
left += int(0.2 * width)
top += int(0.2 * height)
width = int(0.6 * width)
height = int(0.8 * height)
with mss.mss() as sct:
monitor = {"top": top, "left": left, "width": width, "height": height}
screenshot = sct.grab(monitor)
screenshot = np.array(screenshot)
screenshot = cv2.cvtColor(screenshot, cv2.COLOR_BGRA2BGR)
template = cv2.imread(template_path, cv2.IMREAD_COLOR)
gray_screenshot = cv2.cvtColor(screenshot, cv2.COLOR_BGR2GRAY)
gray_template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
gray_screenshot = cv2.GaussianBlur(gray_screenshot, (5, 5), 0)
gray_template = cv2.GaussianBlur(gray_template, (5, 5), 0)
result = cv2.matchTemplate(gray_screenshot, gray_template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
if max_val > 0.7:
icon_x, icon_y = max_loc
icon_center_x = left + icon_x + (template.shape[1]) // 2
icon_center_y = top + icon_y + (template.shape[0]) // 2
pyautogui.click(icon_center_x, icon_center_y)
else:
print('Nie znaleziono odpowiedniego dopasowania')
while True:
find_and_click_blue_icon()
time.sleep(0.3) `
Clicking on blue icon in application
New contributor
Centrum Programisty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.