Using ImageGrab with bbox from pywin32's GetWindowRect

筅森魡賤 提交于 2019-12-11 06:55:11

问题


I want to use PIL's ImageGrab to capture a specific window. What my code below does is that it uses pywin32's FindWindow to get the handle of my wanted window then get its size and location with GetWindowRect. I then use ImageGrab with a bbox equal to the result that I got from GetWindowRect. However, this does not capture the whole window; a big chunk of the window is not shown. What did I do wrong? Here is my code and the result I get:

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

fceuxHWND = win32gui.FindWindow(None, 'FCEUX 2.1.4a: Super Mario Bros. (JU) 
[!]')
rect = win32gui.GetWindowRect(fceuxHWND)
screen = np.array(ImageGrab.grab(bbox=rect), dtype=np.uint8)

cv2.imshow('test',cv2.cvtColor(screen,cv2.COLOR_BGR2RGB))

Result of code


回答1:


Your DPI settings is at 125% and your process is not DPI aware. Call SetProcessDPIAware as follows

import ctypes
...
ctypes.windll.user32.SetProcessDPIAware()


来源:https://stackoverflow.com/questions/51786794/using-imagegrab-with-bbox-from-pywin32s-getwindowrect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!