拍摄屏幕截图并使用OpenCV进行显示时出现的问题

问题描述 投票:0回答:1

当我在台式机上运行此代码时,它运行正常。但是,当我在笔记本电脑上运行它时,当我将图像抓取的边界框设置到Windows计算器的边界框和窗口的屏幕记录并将其自身放置到左上方时,就会出问题。

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

def windowGrab(window_title=None):
    if window_title:
        global hwnd
        hwnd = win32gui.FindWindow(None, window_title)
        if hwnd:
            win32gui.SetForegroundWindow(hwnd)
        else:
            print("window not found")

windowGrab("Calculator")

while True:
    left_x, top_y, right_x, bottom_y = win32gui.GetWindowRect(hwnd)
    screen = np.array(ImageGrab.grab( bbox = (left_x, top_y, right_x, bottom_y ) ) )
    cv2.imshow('window', screen)
    if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break
python numpy opencv pywin32 screen-capture
1个回答
0
投票

这是我在测试您的代码时遇到的问题:

  • 显示计算器窗口无法正常工作。
  • win32gui.FindWindow()找不到正确的窗口,所以我最终将其替换。由于Windows 10的某些原因,win32gui.EnumWindows()列出了一个计算器应用程序的2个窗口:其中一个窗口的宽度/高度值为负。
  • win32gui.GetWindowRect()返回错误的窗口位置和尺寸。似乎认为我的分辨率是1280x720。这可能是因为DPI scaling is being used
  • [ImageGrab.grab()难以使用真实监视器分辨率空间(1920x1080)中的坐标和尺寸来为应用程序截屏。

enter image description here

糟糕:如果目标窗口为最小化,则此应用程序将无法运行。

源代码

import cv2
import numpy as np
import sys

import ctypes
import ctypes.wintypes
from ctypes.wintypes import HWND, RECT, DWORD
from ctypes import *

import win32gui
import win32con

from PIL import ImageGrab


# global variables
dwmapi = ctypes.WinDLL("dwmapi")
APP_NAME = ''
win_hwnd = -1


def callback(hwnd, extra):
    wnd_name = win32gui.GetWindowText(hwnd)

    if (wnd_name == APP_NAME):
        rect = win32gui.GetWindowRect(hwnd)
        x = rect[0]
        y = rect[1]
        w = rect[2] - x
        h = rect[3] - y

        #print("Name: %s" % wnd_name)
        #print("\tLocation: (%d, %d)" % (x, y))
        #print("\t    Size: (%d, %d)" % (w, h))

        if (x >= 0 and y >= 0):
            global win_hwnd
            win_hwnd = hwnd


def windowGrab(window_title=None):
    global APP_NAME, win_hwnd
    APP_NAME = window_title

    if (window_title is None) or (len(window_title) == 0):
        print('!!! window_title == None')
        sys.exit(-1)

    # try to find a window with matching title and valid coordinates
    win32gui.EnumWindows(callback, None)

    # check if it has focus
    if (win_hwnd != win32gui.GetForegroundWindow()):
        print('not focused')
        win32gui.SetActiveWindow(win_hwnd)
        win32gui.SetForegroundWindow(win_hwnd)


# main()
windowGrab("Calculator")

# workaround to allow ImageGrab to capture the whole screen
user32 = ctypes.windll.user32
user32.SetProcessDPIAware()

# get monitor resolution
screen_w = ctypes.windll.user32.GetSystemMetrics(0)
screen_h = ctypes.windll.user32.GetSystemMetrics(1)
print('screen_w=', screen_w, 'screen_h=', screen_h)

# loop
while True:
    # retrieve size and position of the window
    rect = RECT()
    DWMWA_EXTENDED_FRAME_BOUNDS = 9
    dwmapi.DwmGetWindowAttribute(HWND(win_hwnd), DWORD(DWMWA_EXTENDED_FRAME_BOUNDS), ctypes.byref(rect), ctypes.sizeof(rect))

    x = rect.left
    y = rect.top
    w = rect.right- x
    h = rect.bottom - y
    print('x=', x, 'y=', y, 'w=', w, 'h=', h)

    if (w == 0 or h == 0):
        continue

    # take a full screenshot of the desktop
    full_screen = np.array(ImageGrab.grab( bbox= (0, 0, screen_w, screen_h) ))
    if (full_screen is None):
        continue

    # crop window area from the screenshot
    cropped_rgb = full_screen[y : y+h, x : x+w]

    # convert from RGB to BGR order so that colors are displayed correctly
    cropped_bgr = cv2.cvtColor(cropped_rgb, cv2.COLOR_RGB2BGR)

    cv2.imshow('window', cropped_bgr)
    key = cv2.waitKey(25)
    if (key & 0xFF) == ord('q'):
        break

cv2.destroyAllWindows()
© www.soinside.com 2019 - 2024. All rights reserved.