Skip to content

Instantly share code, notes, and snippets.

@Curtis-64
Created September 29, 2023 21:33
Show Gist options
  • Save Curtis-64/8cb84a6e164e6542242ecd4e20cfbaa0 to your computer and use it in GitHub Desktop.
Save Curtis-64/8cb84a6e164e6542242ecd4e20cfbaa0 to your computer and use it in GitHub Desktop.
GUI Element Locator / Grid Locator V2
'Element Locator Code by Curtis White + GPT-4 AD
'Not well tested-- run at own risk. In early tests, it can prevent interaction with system. It works perfectly now, on my system.
'but if you see tha use task manager to kill the kernerl process not from the task manaager but from the task changer layout.
import tkinter as tk
from tkinter import Scale
from pynput import mouse
from PIL import ImageGrab
import cv2
import numpy as np
GRID_SIZE = 75 # Minimum spacing for the crosses
def on_move(x, y):
"""Update mouse coordinates when moved."""
canvas.coords(mouse_text, x, y-30)
canvas.itemconfig(mouse_text, text=f"X: {x}, Y: {y}")
canvas.tag_raise(mouse_text)
def draw_crosses():
# Capture screen
screen = ImageGrab.grab()
screen_np = np.array(screen)
contrast_threshold = contrast_slider.get()
edges = cv2.Canny(screen_np, contrast_threshold, contrast_threshold * 3)
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Sort contours by area, largest first
contours = sorted(contours, key=cv2.contourArea, reverse=True)
selected_points = []
for contour in contours:
if cv2.contourArea(contour) > 100: # Ignore very small contours
M = cv2.moments(contour)
if M["m00"] != 0:
cx = int(M["m10"] / M["m00"])
cy = int(M["m01"] / M["m00"])
# Check if this point is close to other selected points
too_close = any([abs(cx - x) < GRID_SIZE and abs(cy - y) < GRID_SIZE for x, y in selected_points])
if not too_close:
selected_points.append((cx, cy))
markers.append(canvas.create_line(cx-5, cy, cx+5, cy, fill="fuchsia", width=2))
markers.append(canvas.create_line(cx, cy-5, cx, cy+5, fill="fuchsia", width=2))
texts.append(canvas.create_text(cx, cy, text=f"(X={cx},Y={cy})", fill="lime", font=('Arial', 9)))
def WinSet_TransColor(color):
"""Set the transparency color for the window."""
root.attributes('-transparentcolor', color)
def toggle_crosses():
global crosses_visible
crosses_visible = not crosses_visible
if crosses_visible:
draw_crosses()
else:
for mark in markers:
canvas.delete(mark)
for text in texts:
canvas.delete(text)
def close_overlay():
root.quit()
root.destroy()
mouse_listener.stop()
root = tk.Tk()
root.attributes('-fullscreen', True, '-alpha', 1.0) # Set alpha to 1.0 for full visibility
root.overrideredirect(True)
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
transparent_color = '#123456' # A unique color for transparency
root.attributes('-transparentcolor', transparent_color)
canvas = tk.Canvas(root, bg=transparent_color, bd=0, highlightthickness=0)
canvas.pack(fill=tk.BOTH, expand=tk.TRUE)
markers = []
texts = []
crosses_visible = True
mouse_text = canvas.create_text(100, 100, text="", fill="white", font=('Arial', 20))
mouse_listener = mouse.Listener(on_move=on_move)
mouse_listener.start()
close_btn = tk.Button(root, text="Close Overlay", command=close_overlay, bg='white')
close_btn.place(x=screen_width-150, y=10)
toggle_btn = tk.Button(root, text="Toggle Crosses", command=toggle_crosses, bg='white')
toggle_btn.place(x=screen_width-300, y=10)
# Adding a contrast slider
contrast_slider = Scale(root, from_=0, to_=255, orient=tk.HORIZONTAL, label="Contrast Threshold", sliderlength=30, length=200)
contrast_slider.place(x=screen_width-500, y=5)
contrast_slider.set(50) # Default value
contrast_slider.bind("<ButtonRelease-1>", lambda e: draw_crosses()) # Redraw crosses when slider is released
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment