Hussain Sajwani
Hussain Sajwani

Reputation: 13

Using ImageGrab with bbox from pywin32's GetWindowRect

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

Upvotes: 1

Views: 1119

Answers (1)

Barmak Shemirani
Barmak Shemirani

Reputation: 31629

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

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

Upvotes: 1

Related Questions