-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-clicker.py
78 lines (72 loc) · 2.83 KB
/
image-clicker.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
import pyautogui, os, keyboard, datetime, json, cv2
is_logger_active = False
images_path = "images"
key_quit = "esc"
file_extensions = [".png", ".jpg"]
should_return_position = False
grayscale = True
duration = 0.1
confidence = 1.0
def create_folder(name):
try:
os.mkdir(os.getcwd() + "/" + name)
except:
return False
create_folder(images_path)
def strip_image_path(image, images_path=images_path):
return str(image.replace(f"{os.getcwd()}/{images_path}/".replace("\\","/"), ""))
def load_config():
try:
globals_dict = globals()
with open("config.json", "r") as f:
config = json.load(f)
for var in config:
globals_dict[var] = config[var]
if is_logger_active:
print(f"G [{datetime.datetime.now()}] Variable {var} updated to: {globals_dict[var]}")
return(globals())
except FileNotFoundError:
print(f"File config.json not found, using default values...")
except Exception as e:
print(f"Exception: {e}")
def find_image(image, grayscale=True, confidence=1.0):
try:
location = pyautogui.locateOnScreen(image, grayscale=grayscale, confidence=confidence)
return location
except pyautogui.ImageNotFoundException:
if is_logger_active:
print(f"X [{datetime.datetime.now()}] {strip_image_path(image)} not found.")
return False
except Exception as e:
if is_logger_active:
print(f"X [{datetime.datetime.now()}] Except: {e}, {strip_image_path(image)}.")
return False
def click_image(point, duration=duration, tween=pyautogui.easeInSine):
try:
current_pos = pyautogui.position()
pyautogui.moveTo(point, duration=duration, tween=tween)
pyautogui.click()
if should_return_position:
pyautogui.moveTo(current_pos, duration=duration, tween=tween)
if is_logger_active:
print(f"[{datetime.datetime.now()}] Mouse position returned.")
except:
return False
def main():
print(f"[{datetime.datetime.now()}] Script started, searching on your main monitor for images to click. Hold {key_quit} to exit.")
load_config()
is_on = not keyboard.is_pressed(key_quit)
while is_on:
images_dir = os.listdir(f'{os.getcwd()}/{images_path}')
images_list = [
str(f'{os.getcwd()}/{images_path}/{image}').replace("\\","/")
for image in images_dir
if image.endswith(tuple(file_extensions))
]
for image in images_list:
image_pos = find_image(image, grayscale, confidence)
if image_pos is not False:
click_image(image_pos, duration)
print(f"[{datetime.datetime.now()}] {strip_image_path(image)} clicked.")
is_on = not keyboard.is_pressed(key_quit)
main()