Python 辅助 测试版1

代码:

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






# 全局变量
left_top=(0, 0)# 窗口左上角的坐标
arr=[]# 图像匹配到的点
arr_res=[]# 已去相似点




# 截图
def getScreenshots():
    #获取后台窗口的句柄,注意后台窗口不能最小化
    #窗口的类名可以用Visual Studio的SPY++工具获取
    className = "Photo_Lightweight_Viewer"
    titleName = "game-big.jpg - Windows 照片查看器"
    hWnd = win32gui.FindWindow(className, titleName) 
    #获取句柄窗口的大小信息
    left, top, right, bot = win32gui.GetWindowRect(hWnd)
    left_top=(left,top)# 全局变量赋值
    width = right - left
    height = bot - top
    #返回句柄窗口的设备环境,覆盖整个窗口,包括非客户区,标题栏,菜单,边框
    hWndDC = win32gui.GetWindowDC(hWnd)
    #创建设备描述表
    mfcDC = win32ui.CreateDCFromHandle(hWndDC)
    #创建内存设备描述表
    saveDC = mfcDC.CreateCompatibleDC()
    #创建位图对象准备保存图片
    saveBitMap = win32ui.CreateBitmap()
    #为bitmap开辟存储空间
    saveBitMap.CreateCompatibleBitmap(mfcDC,width,height)
    #将截图保存到saveBitMap中
    saveDC.SelectObject(saveBitMap)
    #保存bitmap到内存设备描述表
    saveDC.BitBlt((0,0), (width,height), mfcDC, (0, 0), win32con.SRCCOPY)
    #保存图像 保存bitmap到文件
    saveBitMap.SaveBitmapFile(saveDC,"img_screenshots.jpg")    




# 图片识别返回坐标
def getCoordinates():
    arr=[]
    
    # 截屏保存
    getScreenshots()
    
    # 1. 读入原图和模板    
    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]

    # 归一化平方差匹配
    res=cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
    threshold=0.5

    # 返回res中值大于0.8的所有坐标
    # 返回坐标格式(col,row) 注意:是先col后row 一般是(row,col)!!!
    loc=np.where(res >= threshold)

    # loc:标签/行号索引 常用作标签索引
    # iloc:行号索引
    # loc[::-1]:取从后向前(相反)的元素
    # *号表示可选参数
    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)
        
    # 保存处理后的图片
    cv2.imwrite('img_screenshots_res.png', img_rgb)
    
    # 显示图片 参数:(窗口标识字符串,imread读入的图像)
    #cv2.imshow("test_image", img_rgb)
    return arr




# 判断坐标是否相似
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



# 坐标数组
arr=getCoordinates()
arr_res=[]





# 去相似坐标
for item in arr:
    if isLike(arr_res, item)==False:
        # 转换为真实坐标
        x=item[0]+left_top[0]
        y=item[1]+left_top[1]
        arr_res.append((x,y))
        
                

# 模拟点击
mouse = PyMouse()
for item in arr_res:    
    time.sleep(5) # 休眠1秒
    print(item)
    mouse.click(item[0], item[1])  #移动并且在(x,y)位置左击







# 窗口等待任意键盘按键输入 0为一直等待 其他数字为毫秒数
cv2.waitKey(0)

# 销毁窗口 退出程序
cv2.destroyAllWindows()

game-smart.jpg

game-big.jpg

未完。。。

猜你喜欢

转载自www.cnblogs.com/guxingy/p/12206908.html