python 屏幕找图 点击,使用Python脚本在windows屏幕找图

这篇博客介绍了如何使用Python结合win32gui和OpenCV库在Windows系统上查找特定图像,并实现自动化操作。通过获取窗口句柄,抓取屏幕截图,然后利用模板匹配方法寻找目标图片,从而定位到屏幕上的特定区域。此外,还提到了使用PyQt5进行屏幕抓取,并且在找不到目标图片时设置了一个循环等待。整个过程展示了Python在自动化测试和图像识别中的应用。
摘要由CSDN通过智能技术生成

Python实现windows屏幕找图

import win32gui

hwnd_title = dict()

def get_all_hwnd(hwnd,mouse):

if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):

hwnd_title.update({hwnd:win32gui.GetWindowText(hwnd)})

from PyQt5.QtWidgets import QApplication

from PyQt5.QtGui import *

import win32gui

import sys

import time

import win32gui, win32ui, win32con, win32api

from send_text import send_message_to_slack

import random

import pyscreenshot as ImageGrab

import sys

import numpy as np

import time

from cv2 import cv2

from time import sleep

def get_pay_keyboard_number_location(impath, target,fit_num): #fit_num是匹配度,如 0.95,0.85

print("start find pic")

positions = {}

start = time.time()

img_rgb = cv2.imread(impath)

teNum = "done"

template = cv2.imread(target)

h, w = template.shape[:-1]

res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)

threshold = fit_num # 匹配度参数,1为完全匹配

loc = np.where(res >= threshold)

if len(loc) > 0:

# positions[teNum] = zip(*loc[::-1])[0] # python2的写法

zipped = zip(*loc[::-1]) #list[::-1] 相当于起点为最后的一个,终点为第一个,然后一次减少一个

positions[teNum] = (list(zipped))[0]

else:

print("Can not found pic: ")

end = time.time()

print(end - start)

return positions[teNum]

def zhaotu(fenbianlv):

impath1 = 'screenshot.jpg'

targetPath_a = "yellow" +str(fenbianlv)+'.jpg'

win32gui.EnumWindows(get_all_hwnd, 0)

hwnd = win32gui.FindWindow(None, 'C:\Windows\system32\cmd.exe')

app = QApplication(sys.argv)

screen = QApplication.primaryScreen()

while 1>0:

img = screen.grabWindow(hwnd ).toImage()

img.save("screenshot.jpg")

try:

ls = get_pay_keyboard_number_location(impath1, targetPath_a,0.98) #找目标图

except:

pass

sleep(5)

zhaotu(1792)

Flutter 写的app, 需要源码可以私信~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值