Code:
import cv2 import numpy as np from matplotlib import pyplot as plt import win32gui, win32ui, win32con from ctypes import windll from PIL import Image import time from pymouse import PyMouse # Global variables LEFT_TOP = (0, 0) # top left corner of the window coordinates ARR = [] # image matching the point arr_res = [] # has similarities to # Screenshot DEF getScreenshots (): # get back window handle, pay attention to the background window can not be minimized # window class name can get the SPY ++ using Visual Studio Tools className = " Photo_Lightweight_Viewer " TITLENAME = " Game-big.jpg - Windows Photo Viewer is " the hWnd = win32gui.FindWindow (className, TITLENAME) # acquires size information of the window handle left, Top, right, BOT = win32gui.GetWindowRect (the hWnd) LEFT_TOP = (left, Top) # global variable assignment width = right - left height = BOT - Top # returns a handle to the device context of the window, covering the entire window, including non-client area, title bars, menus, borders hWndDC = win32gui.GetWindowDC (hWnd) # Create a device context mfcDC = win32ui.CreateDCFromHandle (hWndDC) # create a memory device context SaveDC = mfcDC.CreateCompatibleDC () # create a bitmap object ready to save the picture saveBitMap = win32ui.CreateBitmap () # for the bitmap open space saveBitMap.CreateCompatibleBitmap (mfcDC, width, height) # to save the screenshot to the saveBitMap saveDC.SelectObject (saveBitMap) # save the bitmap memory device context saveDC.BitBlt ((0, 0), (width, height), mfcDC, (0, 0), win32con.SRCCOPY) # save save the bitmap image to a file saveBitMap.SaveBitmapFile (SaveDC, " img_screenshots.jpg " ) # Picture identification returns the coordinates DEF GetCoordinates (): arr=[] # 截屏save getScreenshots () # 1 reads the original image and the template img_rgb = cv2.imread ( ' img_screenshots.jpg ' ) img_gray=cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY) template=cv2.imread('game-smart.jpg', 0) h, w=template.shape[:2] # Normalized square difference matching RES = cv2.matchTemplate (img_gray, Template, cv2.TM_CCOEFF_NORMED) threshold=0.5 # Return res coordinate value is greater than 0.8 for all # return coordinate format (col, row) Note: the first is generally col row (Row, COL) !!! LOC = np.where (res> = threshold) # LOC: tags / labels are commonly used as the row number index Index # iLoc: row number Index # LOC [:: -. 1]: taking the element from the front (opposite to) the # represents the number of optional parameters * for Pt in ZIP (* LOC [:: -. 1 ]): right_bottom=(pt[0] + w, pt[1] + h) cv2.rectangle(img_rgb, pt, right_bottom, (0, 0, 255), 2) # 中心点 center_point=(int(pt[0]+w/2), int(pt[1]+h/2)) arr.append(center_point) left2=(center_point[0]-2,center_point[1]-2) right2=(center_point[0]+2,center_point[1]+2) cv2.rectangle(img_rgb, left2, right2, (0, 255, 0), 2) # After saving with pictures cv2.imwrite ( ' img_screenshots_res.png ' , img_rgb) # Show picture identification string parameter :( window, imread read image) # cv2.imshow ( "test_image", img_rgb) return arr # Whether the coordinates are similar DEF isLike (ARR, obj): for Item in ARR: IF (ABS (Item [0] -obj [0]) <. 5) and (ABS (Item [. 1] -obj [. 1]) < 5 ): return True return False # Coordinate array ARR = GetCoordinates () arr_res=[] # To similar coordinates for Item in ARR: IF isLike (arr_res, Item) == False: # converted into real coordinates X = Item [0] + LEFT_TOP [0] y=item[1]+left_top[1] arr_res.append((x,y)) # Simulate a click Mouse = PyMouse () for Item in arr_res: the time.sleep ( . 5) # Sleep one second Print (Item) the Mouse.CLICK (Item [0], Item [ . 1]) # movement and (x, y) position of the left-click # Window wait for any keyboard key input has been waiting for the other numbers 0 to milliseconds cv2.waitKey (0) # Destroy the window exit the program cv2.destroyAllWindows ()
game-smart.jpg
game-big.jpg
Unfinished. . .