I just want to first point out that I am really new at this and most of this code was written by openai but you have to start somewhere right?!
Anyways, I was building this automation script and I noticed that using only pyautogui was problematic because I couldn't really multitask while it was running so I wanted to try using this method instead.
I'm not certain why it's not working but what I think is that it is searching for the image but for some reason it cannot find and click on it. I was going to try and isolate it to a window but couldn't find the window title. I have spent hours trying to get this to work. I even took a new sample of the image to confirm that the target should be a match for the png image.
If anyone can point me in the right direction I would be grateful.
import cv2
import numpy as np
import pyautogui
import random
# Load the template image
template = cv2.imread("C:/DATA/images/events.png", 0)
w, h = template.shape[::-1]
# Capture the screen
screen = np.array(pyautogui.screenshot())
# Convert the screen to grayscale
gray_screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)
# Apply template matching to find the location of the template on the screen
res = cv2.matchTemplate(gray_screen, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where(res >= threshold)
# If a match is found, simulate a mouse click at a random point within the template
if loc[0].size > 0:
x = loc[1][0] + random.randint(0, w)
y = loc[0][0] + random.randint(0, h)
pyautogui.click(x, y)
Python Output:
>>> # If a match is found, simulate a mouse click at a random point within the template
>>> if loc[0].size > 0:
... x = loc[1][0] + random.randint(0, w)
... y = loc[0][0] + random.randint(0, h)
... pyautogui.click(x, y)
This is the original code that I was using:
import pyautogui
import random
import time
# The path to the image file
image_path = "C:/DATA/images/events.png"
# Wait for the image to appear on the screen
image_location = pyautogui.locateOnScreen(image_path)
while image_location is None:
image_location = pyautogui.locateOnScreen(image_path, confidence=0.7)
# Calculate the center of the image
center_x, center_y = pyautogui.center(image_location)
# Add a random offset to the click location
random_offset_x = random.uniform(-5, 5)
random_offset_y = random.uniform(-5, 5)
click_x = center_x + random_offset_x
click_y = center_y + random_offset_y
# Wait for 1000 ms
time.sleep(1)
# Click on the image
pyautogui.click(click_x, click_y)
# Wait for 1000 ms
time.sleep(1)
I also wanted to share this code because I found it very useful though others might think it is terrible it has worked out very well for me. This code opens a command line then runes a python script in command which I found useful because I could divide the code into sections which could be tested individually. You can also open a program with arguments with this bad boy The thing that I found most useful is that this allowed for the code to continue through multiple .py scripts out of a single script and my script continued to run. I'm really new so this was the first avenue that I found to the destination. I tried C++ and couldn't even figure out the dependencies. Yeah, I'm a plumber lol
import subprocess
import time
# events.py
try:
subprocess.run(nox_command, shell=True, check=True)
except subprocess.CalledProcessError as error:
print(f'Error running Nox: {error}')
time.sleep(1)
script_command = "cmd /c start \"\" cmd /c python c:\\DATA\\code\\events.py"
time.sleep(1)
try:
subprocess.run(script_command, shell=True, check=True)
except subprocess.CalledProcessError as error:
print(f'Error running script: {error}')
time.sleep(1)
close_command = "taskkill /f /im cmd.exe"
time.sleep(1)
Anyways, I was building this automation script and I noticed that using only pyautogui was problematic because I couldn't really multitask while it was running so I wanted to try using this method instead.
I'm not certain why it's not working but what I think is that it is searching for the image but for some reason it cannot find and click on it. I was going to try and isolate it to a window but couldn't find the window title. I have spent hours trying to get this to work. I even took a new sample of the image to confirm that the target should be a match for the png image.
If anyone can point me in the right direction I would be grateful.
import cv2
import numpy as np
import pyautogui
import random
# Load the template image
template = cv2.imread("C:/DATA/images/events.png", 0)
w, h = template.shape[::-1]
# Capture the screen
screen = np.array(pyautogui.screenshot())
# Convert the screen to grayscale
gray_screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)
# Apply template matching to find the location of the template on the screen
res = cv2.matchTemplate(gray_screen, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where(res >= threshold)
# If a match is found, simulate a mouse click at a random point within the template
if loc[0].size > 0:
x = loc[1][0] + random.randint(0, w)
y = loc[0][0] + random.randint(0, h)
pyautogui.click(x, y)
Python Output:
>>> # If a match is found, simulate a mouse click at a random point within the template
>>> if loc[0].size > 0:
... x = loc[1][0] + random.randint(0, w)
... y = loc[0][0] + random.randint(0, h)
... pyautogui.click(x, y)
This is the original code that I was using:
import pyautogui
import random
import time
# The path to the image file
image_path = "C:/DATA/images/events.png"
# Wait for the image to appear on the screen
image_location = pyautogui.locateOnScreen(image_path)
while image_location is None:
image_location = pyautogui.locateOnScreen(image_path, confidence=0.7)
# Calculate the center of the image
center_x, center_y = pyautogui.center(image_location)
# Add a random offset to the click location
random_offset_x = random.uniform(-5, 5)
random_offset_y = random.uniform(-5, 5)
click_x = center_x + random_offset_x
click_y = center_y + random_offset_y
# Wait for 1000 ms
time.sleep(1)
# Click on the image
pyautogui.click(click_x, click_y)
# Wait for 1000 ms
time.sleep(1)
I also wanted to share this code because I found it very useful though others might think it is terrible it has worked out very well for me. This code opens a command line then runes a python script in command which I found useful because I could divide the code into sections which could be tested individually. You can also open a program with arguments with this bad boy The thing that I found most useful is that this allowed for the code to continue through multiple .py scripts out of a single script and my script continued to run. I'm really new so this was the first avenue that I found to the destination. I tried C++ and couldn't even figure out the dependencies. Yeah, I'm a plumber lol
import subprocess
import time
# events.py
try:
subprocess.run(nox_command, shell=True, check=True)
except subprocess.CalledProcessError as error:
print(f'Error running Nox: {error}')
time.sleep(1)
script_command = "cmd /c start \"\" cmd /c python c:\\DATA\\code\\events.py"
time.sleep(1)
try:
subprocess.run(script_command, shell=True, check=True)
except subprocess.CalledProcessError as error:
print(f'Error running script: {error}')
time.sleep(1)
close_command = "taskkill /f /im cmd.exe"
time.sleep(1)
Last edited: