python screen-print不捕获所有Windows应用程序的下拉菜单

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python screen-print不捕获所有Windows应用程序的下拉菜单相关的知识,希望对你有一定的参考价值。

我已经测试了三种不同的方法来捕获包含下拉菜单的Windows屏幕。这两种方法都没有捕获下拉菜单。有人可以向我解释如何实现这一目标吗?

要捕获的图像:

enter image description here

捕获所有三种方法后得到的图像

enter image description here

但是,例如Outlook正确捕获下拉列表:

enter image description here

捕获脚本:

import numpy as np
from PIL import ImageGrab
import cv2
import os
import time
import datetime
from mss.windows import MSS as mss
import win32api
import win32gui
import ctypes

user32 = ctypes.windll.user32
dtwidth = user32.GetSystemMetrics(0)
dtheight = user32.GetSystemMetrics(1)

inputkey = {}
inpkeystat = False
mouse_state = 0

CLK_EMPTY = 0
CLK_OK = 1

env = os.environ
try:
    usrenv = env['TEMP']
except:
    usrenv = env['TMP']

BASEDIR = os.path.join(usrenv, './')

def get_click():
    global inputkey
    global mouse_state
    global inpkeystat

    (x, y) = (0, 0)
    wintext = ""
    retcode = CLK_EMPTY

    if inpkeystat == False:
        mouse_state = win32api.GetKeyState(0x01)
        inpkeystat = True

    mouseleft = win32api.GetKeyState(0x01)
    if mouseleft != mouse_state:  # Button state changed
        mouse_state = mouseleft
        if mouseleft < 0:
            # Left Button Pressed
            x, y = win32api.GetCursorPos()
            hwnd = win32gui.WindowFromPoint((x, y))
            wintext = win32gui.GetWindowText(hwnd)
            inpkeystat = False
            retcode = CLK_OK

    return retcode, wintext, (x, y)


logfile = os.path.join(BASEDIR, "scrnprintlog.txt")
date = datetime.datetime.now().strftime("%d-%m-%Y %I:%M%p")
printseq = 1

with open(logfile, "a") as myfile:
    myfile.write("-------------------------------------------
")
    myfile.write(" Log screenprint test
")
    myfile.write(date)
    myfile.write("
-------------------------------------------
")

print "-------------------------------------------"
print " Click mouse on screen to start capture"
print "-------------------------------------------"

while (True):
    clkevent, clkwin, (clkx, clky) = get_click()
    if clkevent == CLK_OK: break

while(True):
    (ax1, ay1, ax2, ay2) = (0, 0, dtwidth, dtheight)

    time.sleep(2)
    #method 1
    try:
        winImage = ImageGrab.grab((ax1, ay1, ax2, ay2))
        imgFile = 'scr-' + str(printseq) + '-pre-imggrab.png'
        imgSave = os.path.join(BASEDIR, imgFile)
        winImage.save(imgSave)

        date = datetime.datetime.now().strftime("%d-%m-%Y %I:%M%p")
        logstring = date + ': ' + imgFile + "
"
        with open(logfile, "a") as myfile:
            myfile.write(logstring)
    except:
        pass

    #method 2
    try:
        sct = mss()
        imgFile = 'scr-' + str(printseq) + '-pre-shot.png'
        imgSave = os.path.join(BASEDIR, imgFile)
        sct.shot(mon=-1, output=imgSave)
        date = datetime.datetime.now().strftime("%d-%m-%Y %I:%M%p")
        logstring = date + ': ' + imgFile + "
"
        with open(logfile, "a") as myfile:
            myfile.write(logstring)
    except:
        pass

    #method 3
    try:
        printscreen_pil =  ImageGrab.grab()
        printscreen_numpy =   np.array(printscreen_pil.getdata(),dtype='uint8')
                        .reshape((printscreen_pil.size[1],printscreen_pil.size[0],3))
        imgFile = 'scr-' + str(printseq) + '-pre-pil.png'
        imgSave = os.path.join(BASEDIR, imgFile)
        cv2.imwrite(imgSave, printscreen_numpy)
        date = datetime.datetime.now().strftime("%d-%m-%Y %I:%M%p")
        logstring = date + ': ' + imgFile + "
"
        with open(logfile, "a") as myfile:
            myfile.write(logstring)
    except:
        pass

    print "-------------------------------------------
"
    print "Click mouse for screen capture
"

    while (True):
        clkevent, clkwin, (clkx, clky) = get_click()
        if clkevent == CLK_OK: break
    print "-------------------------------------------
"

    printseq += 1
答案

MSS 3.1.2开始,这应该是固定的。这个问题是我们如何复制屏幕内容,请参阅完整提交beb43a。更新已在PyPi上提供。

以上是关于python screen-print不捕获所有Windows应用程序的下拉菜单的主要内容,如果未能解决你的问题,请参考以下文章

python—raise异常捕获

python常见异常和异常捕获

在线游戏中打印窗口不捕获

正则表达式捕获逗号分隔值

Python Socket.io 处理所有事件(从客户端捕获所有事件)

Python 3 基础之异常及其捕获