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

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写的

转载于:https://www.cnblogs.com/jokerBi/p/11356938.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值