Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.5 kB
1
Indexable
Never
import cv2
import numpy as np
import pygetwindow as gw
import pyautogui

def process_frame(frame, lower_color, upper_color):
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, lower_color, uppercolor)
    contours,  = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    max_contour = None
    max_area = 0

    for contour in contours:
        area = cv2.contourArea(contour)
        if area > max_area and area < frame.shape[0] * frame.shape[1] / 4:
            max_area = area
            max_contour = contour

    return max_contour

def main():
    lower_color = np.array([100, 100, 100])
    upper_color = np.array([110, 110, 110])

    window_title = 'replace_this'
    window = gw.getWindowsWithTitle(window_title)[0]

    while True:
        screenshot = pyautogui.screenshot(region=(window.left, window.top, window.width, window.height))
        frame = np.array(screenshot)

        target_contour = process_frame(frame, lower_color, upper_color)

        if target_contour is not None and pyautogui.mouseDown(button='right'):
            M = cv2.moments(target_contour)
            cX = int(M["m10"] / M["m00"])
            cY = int(M["m01"] / M["m00"])

            target_x = window.left + cX
            target_y = window.top + cY

            pyautogui.moveTo(target_x, target_y)

        if not pyautogui.mouseDown(button='right'):
            continue

if __name__ == "__main__":
    main()