8

I'm trying to get a full screen (1920 x 1080) capture using this code. The saved images are only 1536 x 864 though.

solution: As Mark pointed out below, Windows has scaling which can be changed via Control Panel > Display (turn it all the way down).

from PIL import ImageGrab
import os
import time

def screenGrab():
    # snapshot of screen
    im = ImageGrab.grab()
    # saves in current work directory with name based on time of pic
    im.save(os.getcwd() + '\\full_snap__' + str(int(time.time()))
            + '.png', 'PNG')

def main():
    screenGrab()

if __name__ == '__main__':
    main()
10
  • 3
    I can't reproduce your problem on my 1920x1200 monitor, it grabs the whole thing. If you print im.size what do you get? Aug 24, 2014 at 4:17
  • (1536, 864). I'm on a laptop @ 1920 x 1080 right now if that makes a difference (that's the native resolution).
    – NoahLE
    Aug 26, 2014 at 4:57
  • Is it possible that you've got your desktop configured for that resolution? Go to Display Properties and verify it. Aug 26, 2014 at 13:16
  • It's definitely 1920 x 1080 on both Windows 8.1 and Mint 17. I did notice that the screenshot resolution is 20% small than my native one. Any settings that could be causing this?
    – NoahLE
    Aug 26, 2014 at 22:30
  • 2
    It's interesting that the text in the window title bar is nice and crisp while the rest of the text looks like it was resized. I suspect Windows is lying to the software, declaring a size of 1536x864 and scaling the output through DWM. If you have any scaling option turned on in the display properties try turning it off and see if it changes anything. Aug 28, 2014 at 19:18

3 Answers 3

10

If you have your Display settings set to anything other than the "smaller" (100%) setting which is the default, Windows will tell your applications to render to a smaller area and then magnify the results as it puts it on the desktop. Evidently PIL has a bug caused by this setting, the capture is being cropped to the smaller size rather than the full desktop. The workaround is to be sure that your display settings are set to 100%.

2
  • Any news about this one? Sep 11, 2021 at 11:29
  • 1
    @DanielReyhanian I don't know, but there's a chance. Development of PIL was stalled for a long time, but then Pillow was forked and lots has happened since. Sep 11, 2021 at 16:22
2

I manage to overcome this issue by adding registry key at

HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers

add a key with the path to your python.exe and pythonw.exe and in value set HIGHDPIAWARE

like so:

"C:\Users\Greg\Anaconda3\python.exe"="HIGHDPIAWARE"

"C:\Users\Greg\Anaconda3\pythonw.exe"="HIGHDPIAWARE"

then everythings should be ok :)

credit to that post: Marking Your Python Program as High DPI Aware Seamlessly Windows

0

I have faced the same issue.. running on 4k screen trying to capture a 1080p app. Thanks to this thread, call the following code before ImageGrab:

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

window_size = get_window_info()
last_time = time.time()
cv2.namedWindow("output", cv2.WINDOW_NORMAL)
while True:
    screen = np.array(ImageGrab.grab(bbox=window_size))
    # print('Frame took {} seconds'.format(time.time()-last_time))
    last_time = time.time()
    # new_screen = process_img(screen)
    imS = cv2.resize(screen, (960, 540))
    cv2.imshow('output', imS)
    # cv2.imshow('window',cv2.cvtColor(screen, cv2.COLOR_BGR2RGB))
    if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break

This solved my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.