使用Python腳本在windows屏幕找圖

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, 需要源碼可以私信~~


最好的筆記軟件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章