Python
1import tkinter as tk 2import win32gui 3import win32ui 4import win32con 5import win32api 6import numpy as np 7import cv2 8from PIL import Image 9import threading 10import time 11 12class AutoClickerGUI:13 def __init__(self, master):14 self.master = master 15 master.title("Auto Clicker")16 17 self.start_button = tk.Button(master, text="Start", command=self.start_auto_clicker)18 self.start_button.pack(pady=10)19 20 self.stop_button = tk.Button(master, text="Stop", command=self.stop_auto_clicker)21 self.stop_button.pack(pady=5)22 23 self.status_label = tk.Label(master, text="")24 self.status_label.pack(pady=5)25 26 self.auto_clicker_running = False27 self.auto_clicker_thread = None28 29 def start_auto_clicker(self):30 if self.auto_clicker_running:31 return32 33 self.auto_clicker_running = True34 self.auto_clicker_thread = threading.Thread(target=self.auto_clicker_loop)35 self.auto_clicker_thread.start()36 37 def stop_auto_clicker(self):38 self.auto_clicker_running = False39 40 def auto_clicker_loop(self):41 window_title = "新しいタブ - Google Chrome"42 you_image_path = "image/you1.png"43 test_image_path = "image/test.png"44 you_click_position = (850, 500)45 test_click_position = (750, 500)46 delay_seconds = 147 48 while self.auto_clicker_running:49 hwnd = win32gui.FindWindow(None, window_title)50 if hwnd == 0:51 self.status_label.config(text="ウィンドウが見つかりません。待機中...")52 time.sleep(1) # ウィンドウが見つかるまで待機53 continue54 else:55 self.status_label.config(text="ウィンドウが見つかりました。")56 57 left, top, right, bottom = win32gui.GetWindowRect(hwnd)58 width = right - left 59 height = bottom - top 60 61 hwndDC = win32gui.GetWindowDC(hwnd)62 mfcDC = win32ui.CreateDCFromHandle(hwndDC)63 saveDC = mfcDC.CreateCompatibleDC()64 saveBitMap = win32ui.CreateBitmap()65 saveBitMap.CreateCompatibleBitmap(mfcDC, width, height)66 saveDC.SelectObject(saveBitMap)67 saveDC.BitBlt((0, 0), (width, height), mfcDC, (0, 0), win32con.SRCCOPY)68 69 # オリジナルのスクリーンショットを保存70 original_image = self.capture_window_image(hwnd, width, height)71 cv2.imwrite("original_screenshot.png", original_image)72 73 bmpinfo = saveBitMap.GetInfo()74 bmpstr = saveBitMap.GetBitmapBits(True)75 im = Image.frombuffer("RGB", (width, height), bmpstr, "raw", "BGR", 0, 1)76 im_cv = cv2.cvtColor(np.array(im), cv2.COLOR_RGB2BGR)77 78 # スクリーンショットを保存79 cv2.imwrite("screenshot.png", im_cv)80 81 test_found, test_image = self.find_in_window(im_cv, test_image_path)82 if test_found:83 print("Test image found")84 self.click_at_position(hwnd, test_click_position)85 self.status_label.config(text="Clicked at position for: test.png")86 print("test")87 cv2.imwrite("test_found.png", test_image)88 else:89 you_found, you_image = self.find_in_window(im_cv, you_image_path)90 if you_found:91 print("You image found")92 self.click_at_position(hwnd, you_click_position)93 self.status_label.config(text="Clicked at position for: you.png")94 print("you")95 cv2.imwrite("you_found.png", you_image)96 else:97 print("No image found")98 self.status_label.config(text="No image found. Waiting...")99 return100 101 time.sleep(delay_seconds)102 103 def capture_window_image(self, hwnd, width, height):104 hwndDC = win32gui.GetWindowDC(hwnd)105 mfcDC = win32ui.CreateDCFromHandle(hwndDC)106 saveDC = mfcDC.CreateCompatibleDC()107 saveBitMap = win32ui.CreateBitmap()108 saveBitMap.CreateCompatibleBitmap(mfcDC, width, height)109 saveDC.SelectObject(saveBitMap)110 saveDC.BitBlt((0, 0), (width, height), mfcDC, (0, 0), win32con.SRCCOPY)111 bmpinfo = saveBitMap.GetInfo()112 bmpstr = saveBitMap.GetBitmapBits(True)113 return cv2.cvtColor(np.array(Image.frombuffer("RGB", (bmpinfo['bmWidth'], bmpinfo['bmHeight']), bmpstr, "raw", "BGRX", 0, 1)), cv2.COLOR_BGR2RGB)114 115 def find_in_window(self, image, template_path):116 # 画像の読み込み117 template = cv2.imread(template_path, cv2.IMREAD_GRAYSCALE)118 if template is None:119 print(f"Error: Unable to load template image: {template_path}")120 return False, None121 122 # 特徴点の検出123 sift = cv2.SIFT_create()124 kp1, des1 = sift.detectAndCompute(template, None)125 kp2, des2 = sift.detectAndCompute(image, None)126 127 if des1 is None or des2 is None:128 print("Error: Failed to detect keypoints and descriptors.")129 return False, None130 131 # 特徴点のマッチング132 bf = cv2.BFMatcher()133 matches = bf.knnMatch(des1, des2, k=2)134 135 # マッチングが成功した特徴点を取得136 good_matches = []137 for m, n in matches:138 if m.distance < 0.75 * n.distance:139 good_matches.append(m)140 141 print("Number of good matches:", len(good_matches))142 143 if len(good_matches) > 10:144 return True, template # マッチングした画像を返す145 146 return False, None147 148 def click_at_position(self, hwnd, position):149 left, top, _, _ = win32gui.GetWindowRect(hwnd)150 lParam = win32api.MAKELONG(left + position[0], top + position[1])151 win32api.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam)152 win32api.SendMessage(hwnd, win32con.WM_LBUTTONUP, 0, lParam)153 print("Clicking at position:", position)154 155root = tk.Tk()156my_gui = AutoClickerGUI(root)157root.mainloop()

0 コメント