全屏游戏截图失败

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

这适用于普通窗口,但在全屏游戏中会返回黑屏,因此我将

dtype = numpy.uint8
更改为
dtype = numpy.uint16
,现在我收到此错误。我认为这与元组中的 4 有关。

File "C:\Users\phpjunkie\Python\test_debug\testing\test12.py", line 22, in CaptureWwindow
output.shape = (height, width, 4)
^^^^^^^^^^^^
ValueError: cannot reshape array of size 12288000 into shape (1600,3840,4)

这是代码:

import phpjunkie.Win32 as Win32
import cv2 as cv
import numpy
import win32gui
import win32ui
import win32con


def CaptureWwindow(hwnd: int):
    left, top, right, bottom = win32gui.GetWindowRect(hwnd)
    width, height = right - left,  bottom - top

    wdc = win32gui.GetWindowDC(hwnd)
    dc = win32ui.CreateDCFromHandle(wdc)
    cdc = dc.CreateCompatibleDC()
    bitmap = win32ui.CreateBitmap()
    bitmap.CreateCompatibleBitmap(dc, width, height)
    cdc.SelectObject(bitmap)
    cdc.BitBlt((0, 0), (width, height), dc, (0, 0), win32con.SRCCOPY)

    output = numpy.frombuffer(bitmap.GetBitmapBits(True), dtype = numpy.uint16)
    output.shape = (height, width, 4)

    dc.DeleteDC()
    cdc.DeleteDC()
    win32gui.ReleaseDC(hwnd, wdc)
    win32gui.DeleteObject(bitmap.GetHandle())

    return output


hwnd = Win32.GetWindowHandle(partial = 'Marvel\'s Spider-Man Remastered')
screenshot = CaptureWwindow(hwnd)
screenshot = (screenshot >> 2).astype(numpy.uint8)

cv.imshow('screenshot', screenshot)
print(screenshot.dtype)
print(screenshot.shape)
cv.waitKey()
python winapi pywin32 screen-capture game-automation
1个回答
0
投票

我看到了别人的代码并且看到了需要添加的三行,这给了我我想要的东西,那就是获得游戏本身的屏幕截图而不是桌面的屏幕截图。

import cv2 as cv
import numpy
from ctypes import windll # first line
import win32gui
import win32ui
import win32con
from pyvda import AppView, get_apps_by_z_order


def CaptureWwindow(hwnd: int) -> numpy.ndarray:
    if not bool(hwnd):
        return numpy.array([])

    windll.user32.SetProcessDPIAware() # second line

    left, top, right, bottom = win32gui.GetWindowRect(hwnd)
    width, height = right - left, bottom - top

    wdc = win32gui.GetWindowDC(hwnd)
    dc = win32ui.CreateDCFromHandle(wdc)
    cdc = dc.CreateCompatibleDC()
    bitmap = win32ui.CreateBitmap()
    bitmap.CreateCompatibleBitmap(dc, width, height)
    cdc.SelectObject(bitmap)
    cdc.BitBlt((0, 0), (width, height), dc, (0, 0), win32con.SRCCOPY)

    windll.user32.PrintWindow(hwnd, cdc.GetSafeHdc(), 3) # third line

    output = numpy.frombuffer(bitmap.GetBitmapBits(True), dtype = numpy.uint8)
    output.shape = (height, width, 4)

    dc.DeleteDC()
    cdc.DeleteDC()
    win32gui.ReleaseDC(hwnd, wdc)
    win32gui.DeleteObject(bitmap.GetHandle())

    return output


window = [window for window in get_apps_by_z_order(current_desktop = False) if 'Marvel\'s Spider-Man Remastered' in win32gui.GetWindowText(window.hwnd)]
if bool(len(window)):
    window = window[0]

if isinstance(window, AppView):
    screenshot = CaptureWwindow(window.hwnd)
    cv.imshow(winname = 'screenshot', mat = screenshot)
    cv.waitKey()

我发现他的代码的唯一问题是

win32gui.GetClientRect(hwnd)
而不是
win32gui.GetWindowRect(hwnd)
生成全零。

© www.soinside.com 2019 - 2024. All rights reserved.