채팅0

    쇄초 파이썬 대충 이런느낌 코드 gpt랑 제작해봐

    조회수 141

    import sys
    import cv2
    import numpy as np
    import pyautogui
    import time
    from PyQt5.QtWidgets import QApplication, QWidget
    from PyQt5.QtCore import Qt, QTimer
    from PyQt5.QtGui import QPainter, QImage, QPixmap
    import win32gui
    import win32con

    # 설정값
    block_size = 48
    capture_region = (160, 45, 1350, 830)
    offset_x = 0
    offset_y = 23
    timeout_ms = 1000
    max_move_px = 40

    # 빨탭 고정 HSV 필터
    lower_hsv = np.array([0, 160, 240])
    upper_hsv = np.array([5, 190, 255])

    class Overlay(QWidget):
       def __init__(self):
           super().__init__()
           self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.Tool)
           hwnd = int(self.winId())
           ex_style = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
           win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE,
               ex_style | win32con.WS_EX_LAYERED | win32con.WS_EX_TRANSPARENT | win32con.WS_EX_TOOLWINDOW)
           self.setAttribute(Qt.WA_TranslucentBackground)
           self.setGeometry(0, 0, pyautogui.size().width, pyautogui.size().height)
           self.cx, self.cy = -1, -1
           self.last_seen = None
           self.last_seen_time = 0

           # 이미지 불러오기
           self.images = {
               (0, -4): QPixmap("1.png"),  # 위
               (0,  4): QPixmap("3.png"),  # 아래
               (-4, 0): QPixmap("4.png"),  # 왼쪽
               ( 4, 0): QPixmap("2.png"),  # 오른쪽
           }

           QTimer.singleShot(0, self.start_timer)

       def start_timer(self):
           self.timer = QTimer(self)
           self.timer.timeout.connect(self.update_overlay)
           self.timer.start(60)

       def update_overlay(self):
           screenshot = pyautogui.screenshot(region=capture_region)
           frame = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2BGR)
           hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

           red_mask = cv2.inRange(hsv, lower_hsv, upper_hsv)
           kernel = np.ones((3, 3), np.uint8)
           red_mask = cv2.dilate(red_mask, kernel, iterations=3)

           contours, _ = cv2.findContours(red_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
           now = time.time() * 1000

           best_candidate = None
           max_area = 0

           for contour in contours:
               x, y, w, h = cv2.boundingRect(contour)
               area = w * h
               if 5 < w < 120 and 5 < h < 120:
                   if area > max_area:
                       max_area = area
                       best_candidate = (x, y, w, h)

           if best_candidate:
               x, y, w, h = best_candidate
               new_cx = x + w // 2 + capture_region[0] + offset_x
               new_cy = y + h // 2 + capture_region[1] + offset_y

               if self.cx != -1 and self.cy != -1:
                   dx = abs(new_cx - self.cx)
                   dy = abs(new_cy - self.cy)
                   if dx > max_move_px or dy > max_move_px:
                       if self.last_seen and (now - self.last_seen_time < timeout_ms):
                           self.cx, self.cy = self.last_seen
                           return
                       else:
                           self.cx, self.cy = -1, -1
                           return

               self.cx, self.cy = new_cx, new_cy
               self.last_seen = (new_cx, new_cy)
               self.last_seen_time = now
           else:
               if self.last_seen and (now - self.last_seen_time < timeout_ms):
                   self.cx, self.cy = self.last_seen
               else:
                   self.cx, self.cy = -1, -1

           self.update()

       def paintEvent(self, event):
           if self.cx == -1 or self.cy == -1:
               return
           painter = QPainter(self)
           directions = [(0, -4), (0, 4), (-4, 0), (4, 0)]
           for dx, dy in directions:
               x = self.cx + dx * block_size - block_size // 2
               y = self.cy + dy * block_size - block_size // 2
               img = self.images[(dx, dy)]
               painter.drawPixmap(x, y, block_size, block_size, img)

    app = QApplication(sys.argv)
    overlay = Overlay()
    overlay.show()
    app.exec_()
     

    댓글1

    회원프로필

    픽셀 사이즈 등 다 조정해놓은거라 저거 그대로 쓰면서 수정하면됨

    2025.05.31 23:33