python 实现循环查找桌面图片并双击

'''
opencv 屏幕找图并点击案例
还要用到python中图像处理识别的相关库,主要有pillow和open-cv。可使用pip命令安装相应的包;
预先截取好想点击区域的色块,存放在目录之下。
https://blog.csdn.net/wz2671/article/details/102751549
准备安装:
pip3 install pillow      ok
pip3 install opencv-python     ok

pip3 install pyautogui ok

建议使用 python -m pip install pyautogui  进行安装

本次测试python版本3.7.3

'''

import pyautogui
import cv2
import numpy as np
from pymouse import *
m=PyMouse()

'''截屏桌面'''
def saveScreen():
    x_dim, y_dim = m.screen_size()
    img = pyautogui.screenshot(region=[0,0,x_dim,y_dim]) # x,y,w,h
    img.save('screenshot1.png')
    '''numpy'''
    img = cv2.cvtColor(np.asarray(img),cv2.COLOR_RGB2BGR)


def find_button(target, template):
    """
    寻找target图片在template中的位置,返回应该点击的坐标。
    """
    theight,twidth = target.shape[:2]
    # 执行模板匹配,采用的匹配方式cv2.TM_SQDIFF_NORMED
    result = cv2.matchTemplate(target, template, cv2.TM_SQDIFF_NORMED)

    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
    # 如果匹配度小于99%,就认为没有找到。
    if min_val > 0.01:
        return None
    strmin_val = str(min_val)
    print(strmin_val)
    # 绘制矩形边框,将匹配区域标注出来

    # cv2.rectangle(template, min_loc, (min_loc[0] + twidth, min_loc[1] + theight), (0, 0, 225), 2)
    # cv2.imshow("MatchResult----MatchingValue="+strmin_val, template)
    # cv2.imwrite('1.png', template, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
    # cv2.waitKey()
    # cv2.destroyAllWindows()
    x = min_loc[0] + twidth//3
    y = min_loc[1] + theight//3
    return (x, y)

'''主程序 run_times 用来计数 run_times_max 配置最大检查次数 跑多少次停止'''
run_times=0
run_times_max=5

while True:
    saveScreen()
    temp = cv2.imread('d://tmp//screenshot1.png')
    target = cv2.imread('d://tmp//target.PNG')
    print (target.shape[:2])
    print (temp.shape[:2])
    result=find_button(target,temp)
    if result is not None:
        print('findposition',result)
        m.click(result[0],result[1],1,2)
    else:
        print('not found')
    import time
    print ("Start : %s" % time.ctime())
    time.sleep(5)
    print ("End : %s" % time.ctime())
    #print ('findposition',find_button(target,temp))
    run_times+=1
    if run_times==run_times_max:
        print("run times",run_times)
        break

'''
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import *
import win32gui
import sys
 
hwnd = win32gui.FindWindow(None, 'C:\Windows\system32\cmd.exe')
app = QApplication(sys.argv)
screen = QApplication.primaryScreen()
img = screen.grabWindow(hwnd).toImage()
img.save("screenshot.jpg")


import pyautogui
import cv2
 
img = pyautogui.screenshot(region=[0,0,100,100]) # x,y,w,h
# img.save('screenshot.png')
img = cv2.cvtColor(np.asarray(img),cv2.COLOR_RGB2BGR)

numpy install 
pip install numpy -i https://pypi.mirrors.ustc.edu.cn/simple/
'''

你可能感兴趣的:(python3,opencv,python,pip)