import cv2 import numpy as np import pyautogui import time import random import tkinter as tk from PIL import ImageGrab import keyboard # Define ROI (Region of Interest) roi_top = 100 roi_left = 100 roi_width = 500 roi_height = 500 # Define minimum and maximum delay times in seconds min_delay = 0.05 # Minimum delay between screenshots max_delay = 0.1 # Maximum delay between screenshots # Create a transparent window root = tk.Tk() root.geometry(f"{roi_width}x{roi_height}+{roi_left}+{roi_top}") root.attributes('-alpha', 0.3) # Set transparency root.overrideredirect(True) # Remove the window border def move_window(event): root.geometry(f"+{event.x_root}+{event.y_root}") # Bind mouse movement to window positioning root.bind('', move_window) try: while True: if keyboard.is_pressed('esc'): print("Program terminated by user") break delay = random.uniform(min_delay, max_delay) time.sleep(delay) # Capture the screen within the ROI screenshot = ImageGrab.grab(bbox=(root.winfo_x(), root.winfo_y(), roi_left + roi_width, root.winfo_y() + roi_height)) image = np.array(screenshot) # Convert the PIL image to an OpenCV format image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # Define the color range you are looking for lower = np.array([28, 253, 130]) # Lower bound of the color range upper = np.array([88, 255, 190]) # Upper bound of the color range # Create a mask for the specified color range mask = cv2.inRange(image, lower, upper) # Find coordinates of pixels within the specified color range y, x = np.where(mask == 255) if len(x) > 0 and len(y) > 0: pyautogui.click(root.winfo_x() + x[-1], root.winfo_y() + y[-1]) print(f"Clicked at ({root.winfo_x() + x[-1]}, {root.winfo_y() + y[-1]})") # Update the window to reflect changes root.update_idletasks() root.update() except KeyboardInterrupt: print("Program terminated by user")