Win32gui的Hwnd问题和映像未传递给主

问题描述

我正在尝试制作一个程序,该程序将从应用程序中捕获特定窗口并将其显示在新窗口中(稍后,我想在第二个窗口中执行一些OpenCV任务。该程序昨天本身已成功运行,但是今天我似乎收到了错误

“ pywintypes.error:(6,'SetForegroundWindow','句柄无效。')”

我不确定为什么会弹出此错误,因为自上次成功运行以来我没有更改代码

“ Screencap”类应该将图像(称为“图像”)返回给main,但是在main中出现错误,指出未定义“ image”。我设置了一个名为image = None的新变量,发现没有来自'Screencap'的图像传递给main。我对这两个问题感到困惑。

很抱歉,如果解决方案很简单,我将重新开始编程。预先感谢您的帮助^ _ ^

Main.py:

import numpy as np
import cv2
from Stream import Screencap

image = None
Stream = Screencap()
Stream.Getwindow('discord')

while 1:                                                    
    Screenshot = Stream.GetScreencap()
    image = np.array(image)
    print(image)
    image = cv2.cvtColor(image,cv2.COLOR_RGB2BGR)
    cv2.imshow("Screen",image)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

Stream.py:

# This class will be responsible for taking a screenshot of an application 

import cv2
import numpy as np
from PIL import ImageGrab
import win32gui

class Screencap:

    def __init__(self):                                                                             
        self.hwnd = None

    def Getwindow(self,TargetName):                                    # This function allows you to select which windows you want to capture  
        
        WindowOption = []                                                   
        WindowNameList = []
        toplist = []

        def enum_win(hwnd,result):                                     # We get all the window handle numbers (hwnd) and names for all active windows
            WindowName = win32gui.GetwindowText(hwnd)
            WindowNameList.append((hwnd,WindowName))
            
        win32gui.EnumWindows(enum_win,toplist) 

        for (hwnd,WindowName) in WindowNameList:                       # We look though all the names of the window names. If it has the key phrase we are 
            if WindowName.find(TargetName) != -1:                       # searching for (TargetName),we save that window's name and hwnd
                print((len(WindowOption)),": ",hwnd,WindowName)      # If we don't find a match the function returns a -1
                WindowOption.append((hwnd,WindowName))                 # If hwnd is None then we capture the desktop. If we kNow there a window match,we save 
                self.hwnd = hwnd                                        # any hwnd so it's no longer None
                                                                        
        if self.hwnd != None:                                           # If we find a window(s),we ask the user which window they want to capture
            choice = input('What number is your window?\n')             # In case the user wants to record a YouTube video,but the user has 2 seperate 
            choice = int(choice)                                        # windows open with YouTube being the active tab,the user can choose what they want
            print(WindowOption[choice][0])
            self.hwnd = WindowOption[choice][0]
        else:                                                           # If no hwnd is selected and hwnd is still None,we tell the user we will capture the
            print("\nCan't find application \nDesktop Selected\n")      # desktop

    def GetScreencap(self):                                             # This fuction gets a screenshot of the window
                                                                        # If we have a hwnd,we make the window visible,find the pixel location of the window 
        if self.hwnd != None:                                           # and then take a screenshot within that region
            win32gui.SetForegroundWindow(self.hwnd)
            res = win32gui.GetwindowRect(self.hwnd)
            image = ImageGrab.grab(res)
        else:                                                           # If we don't have a hwnd,we take a screenshot of our monitor and we don't crop it
            image = ImageGrab.grab()

        return image

        # image = np.array(image)
        # image = cv2.cvtColor(image,cv2.COLOR_RGB2BGR)
        # cv2.imshow("Screen",image)
        # if cv2.waitKey(1) & 0xFF == ord('q'):
        #   break

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...