Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why cannot ImageGrab.grab capture the whole screen? [duplicate]

I was trying to capture my screen image using PIL.ImageGrab.grab(). Here is my problem -- When I use the code below, img is only the upper left part of my screen.

from PIL import ImageGrab
img = ImageGrab.grab()

Use win32api.GetSystemMetrics() to find out my screen size.

> GetSystemMetrics(0)
Out[6]: 1280

> GetSystemMetrics(1)
Out[7]: 720

I then used ImageGrab.grab((0,0,1280,720)), and still got the upper left part of my screen! Desperately, I called ImageGrab.grab((0,0,1400,900)), and the output is the same partial image with a black frame in its lower right area...

I have no idea what happened. It seems other guys are able to capture their screen by simply call ImageGrab.grab().

Any help would be appreciated!

like image 413
H.Zhao Zhang Avatar asked Sep 02 '25 06:09

H.Zhao Zhang


2 Answers

As josh pointed out, There is a working workaround for this without fiddling with the OS settings. The solution is to use the following to make your program DPI aware on Windows:

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

by josh

like image 120
Elijas Dapšauskas Avatar answered Sep 04 '25 20:09

Elijas Dapšauskas


I faced the same problem on PIL when I was trying to follow this tutorial https://code.tutsplus.com/tutorials/how-to-build-a-python-bot-that-can-play-web-games--active-11117.

To solve this problem,

-> Goto Python installed directory and Right-click on python.exe

-> Properties -> Compatibility tab -> check 'Disable display scaling on high DPI settings'.

Repeat the same procedure for pythonw.exe. Hope your problem solved. Please let me know.

like image 25
MD KAMRUL HASAN SHAHED Avatar answered Sep 04 '25 19:09

MD KAMRUL HASAN SHAHED