-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathImage-Clicker(v1.2).py
113 lines (91 loc) · 3.99 KB
/
Image-Clicker(v1.2).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# | Made by 2cz5 | https://github.com/2cz5 | Discord:2cz5 (for questions etc..)
import cv2
import numpy as np
import pyautogui
import threading
import time
import win32gui
import win32con
import keyboard
import os
import logging
# Global variable to indicate if the killswitch is activated
killswitch_activated = False
# Set up logging
logging.basicConfig(filename='clicker.log', level=logging.INFO,
format='%(asctime)s - %(levelname)s: %(message)s')
# Function to minimize the command prompt window (Windows-specific)
def minimize_cmd_window():
try:
# Find the command prompt window by its class name
hwnd = win32gui.FindWindow("ConsoleWindowClass", None)
if hwnd != 0:
win32gui.ShowWindow(hwnd, win32con.SW_MINIMIZE)
except Exception as e:
logging.error(f"Error minimizing command prompt window: {e}")
# Function to monitor the killswitch key
def monitor_killswitch(killswitch_key):
global killswitch_activated
while True:
if keyboard.is_pressed(killswitch_key):
logging.info("Killswitch activated.")
killswitch_activated = True
break
time.sleep(0.1)
# Function to search for images on the screen and click on them if found
def search_and_click(images, threshold=0.8, click_delay=0.01, killswitch_key='q'):
# Set the template matching method
method = cv2.TM_CCOEFF_NORMED
# Start monitoring the killswitch key in a separate thread
killswitch_thread = threading.Thread(target=monitor_killswitch, args=(killswitch_key,))
killswitch_thread.start()
while not killswitch_activated:
minimize_cmd_window() # Minimize the command prompt window
# Capture the screen image
screenshot = pyautogui.screenshot()
screen_np = np.array(screenshot)
screen_gray = cv2.cvtColor(screen_np, cv2.COLOR_RGB2GRAY)
# Iterate through each image in the database
for image_path in images:
if not os.path.exists(image_path):
logging.error(f"Image not found at '{image_path}'")
continue # Skip to the next image if the file doesn't exist
# Load the image from the database
template = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
# Perform template matching
result = cv2.matchTemplate(screen_gray, template, method)
# Get the location of matches above the specified threshold
loc = np.where(result >= threshold)
# Click on the matched locations
if loc[0].size > 0:
for pt in zip(*loc[::-1]):
# Calculate the center of the matched template
x, y = pt[0] + template.shape[1] // 2, pt[1] + template.shape[0] // 2
# Click on the center of the matched template
pyautogui.click(x, y)
logging.info(f"Clicked on {image_path} at ({x}, {y})")
time.sleep(click_delay) # Delay between clicks
# Check if killswitch is activated after each click
if killswitch_activated:
break
# Check if killswitch is activated after processing each image
if killswitch_activated:
break
# Check if killswitch is activated after processing all images
if killswitch_activated:
break
logging.info("Exiting the loop.")
# Main function to execute the script
def main():
# List of image paths to search for on the screen
# Replace these with the paths to your actual images
image_paths = [
r"C:\path\to\image1.png",
r"C:\path\to\image2.png",
# Add more image paths as needed
]
# Call the function with the list of image paths and optional parameters
search_and_click(image_paths)
# Entry point of the script
if __name__ == "__main__":
main()