码迷,mamicode.com
首页 > 编程语言 > 详细

python+opencv+pil实现windows 图片位置查找

时间:2019-08-15 13:09:23      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:name   open   res   tool   return   ISE   函数   pyw   voice   

PIL系统截图、cv2图片匹配

from PIL import ImageGrab
import cv2
import numpy as np

from utils.windows import mouseMove, mouseClick


def mathc_img(Target, value = 0.9):
    try:
        im = np.array(ImageGrab.grab())
        img_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        template = cv2.imread(Target, 0)
        res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
        threshold = value
        loc = np.where(res >= threshold)
        return (int(loc[1][0]), int(loc[0][0]))
    except :
        raise Exception('未匹配到图片')


def imageSearchClick(Target, x_, y_):
    mouseMove(0, 0)
    x, y = mathc_img(Target)
    mouseMove(x + x_, y + y_)
    mouseClick()
    mouseMove(0, 0)



if __name__ == '__main__':
    # x, y = mathc_img(r'C:\Users\btc\Desktop\1.PNG')
    # mouseMove(x, y)
    imageSearchClick(r'C:\invoicePrint\images\tool0.PNG', 50, 30)

其中的 mouseClick和mouseMove是我自己定义的操作鼠标函数,采用pywin32写的

python+opencv+pil实现windows 图片位置查找

标签:name   open   res   tool   return   ISE   函数   pyw   voice   

原文地址:https://www.cnblogs.com/jokerBi/p/11356938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!