Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 python屏幕打印不会捕获所有windows应用程序上的下拉菜单_Python 2.7_Windows 7_Screenshot_Windows 10 Desktop_Python Mss - Fatal编程技术网

Python 2.7 python屏幕打印不会捕获所有windows应用程序上的下拉菜单

Python 2.7 python屏幕打印不会捕获所有windows应用程序上的下拉菜单,python-2.7,windows-7,screenshot,windows-10-desktop,python-mss,Python 2.7,Windows 7,Screenshot,Windows 10 Desktop,Python Mss,我已经测试了三种不同的方法来捕获包含下拉菜单的windows屏幕。这两种方法都无法捕获下拉菜单。有人能向我解释一下这是如何实现的吗 要捕获的图像: 三种方法捕获后的结果图像 但是,例如在Outlook上,下拉列表被正确捕获: 捕获脚本: import numpy as np from PIL import ImageGrab import cv2 import os import time import datetime from mss.windows import MSS as mss

我已经测试了三种不同的方法来捕获包含下拉菜单的windows屏幕。这两种方法都无法捕获下拉菜单。有人能向我解释一下这是如何实现的吗

要捕获的图像:

三种方法捕获后的结果图像

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

捕获脚本:

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("-------------------------------------------\n")
    myfile.write(" Log screenprint test\n")
    myfile.write(date)
    myfile.write("\n-------------------------------------------\n")

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 + "\n"
        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 + "\n"
        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 + "\n"
        with open(logfile, "a") as myfile:
            myfile.write(logstring)
    except:
        pass

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

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

    printseq += 1
将numpy导入为np
从PIL导入ImageGrab
进口cv2
导入操作系统
导入时间
导入日期时间
从mss.windows将mss作为mss导入
导入win32api
导入win32gui
导入ctypes
user32=ctypes.windell.user32
dtwidth=user32.GetSystemMetrics(0)
dtheight=user32.GetSystemMetrics(1)
inputkey={}
inpkeystat=False
鼠标状态=0
CLK_EMPTY=0
CLK_OK=1
env=os.environ
尝试:
usrenv=env['TEMP']
除:
usrenv=env['TMP']
BASEDIR=os.path.join(usrenv,'./'))
def get_click():
全局输入键
全局鼠标状态
全局输入键值
(x,y)=(0,0)
wintext=“”
retcode=时钟为空
如果inpkeystat==False:
鼠标状态=win32api.GetKeyState(0x01)
inpkeystat=True
mouseleft=win32api.GetKeyState(0x01)
如果鼠标掉了!=鼠标状态:#按钮状态已更改
mouse_state=mouseleft
如果mouseleft<0:
#按左键
x、 y=win32api.GetCursorPos()
hwnd=win32gui.WindowFromPoint((x,y))
wintext=win32gui.GetWindowText(hwnd)
inpkeystat=False
retcode=CLK\U正常
返回retcode,wintext,(x,y)
logfile=os.path.join(BASEDIR,“scrnprintlog.txt”)
date=datetime.datetime.now().strftime(“%d-%m-%Y%I:%m%p”)
printseq=1
打开(日志文件,“a”)作为我的文件:
myfile.write(“---------------------------------------\n”)
myfile.write(“日志屏幕打印测试\n”)
myfile.write(日期)
myfile.write(“\n-------------------------------------------\n”)
打印“---------------------------------------”
打印“在屏幕上单击鼠标开始捕获”
打印“---------------------------------------”
虽然(正确):
clkevent,clkwin,(clkx,clky)=点击获取
如果clkevent==CLK_正常:中断
虽然(正确):
(ax1,ay1,ax2,ay2)=(0,0,dtwidth,dtheight)
时间。睡眠(2)
#方法1
尝试:
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”)
日志字符串=日期+':'+imgFile+“\n”
打开(日志文件,“a”)作为我的文件:
myfile.write(日志字符串)
除:
通过
#方法2
尝试:
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”)
日志字符串=日期+':'+imgFile+“\n”
打开(日志文件,“a”)作为我的文件:
myfile.write(日志字符串)
除:
通过
#方法3
尝试:
printscreen_pil=ImageGrab.grab()
printscreen\u numpy=np.array(printscreen\u pil.getdata(),dtype='uint8')\
.重塑((打印屏幕尺寸[1],打印屏幕尺寸[0],3))
imgFile='scr-'+str(printseq)+'-pre-pil.png'
imgSave=os.path.join(BASEDIR,imgFile)
cv2.imwrite(imgSave,打印屏幕)
date=datetime.datetime.now().strftime(“%d-%m-%Y%I:%m%p”)
日志字符串=日期+':'+imgFile+“\n”
打开(日志文件,“a”)作为我的文件:
myfile.write(日志字符串)
除:
通过
打印“---------------------------------------\n”
打印“单击鼠标进行屏幕捕获\n”
虽然(正确):
clkevent,clkwin,(clkx,clky)=点击获取
如果clkevent==CLK_正常:中断
打印“---------------------------------------\n”
printseq+=1
从开始,这应该是固定的。这个问题是我们如何复制屏幕内容,请参阅完整提交。PyPi上已经提供了更新。

小提示:,如果答案对您有利:)