Nov/09
20
Python Print Screen and Save Image in Windows
0 Comments | Posted by Spechal in Python, Windows
So you need to do a “Print Screen” and save the image in the clipboard huh? I had that exact need too. Let me save you a few hours of trouble and tell you that the win32clipboard does not yet have the ability to return the contents of the clipboard when the contents are BITMAP, despite the CF_BITMAP constant.
How do you do it then? I was wondering the same thing. Behold, PIL. PIL, the Python Image Library, saved me from a massive headache.
Here is the code to take a screen shot and save it. You will need the win32api and PIL libraries.
import win32api, win32con, ImageGrab
win32api.keybd_event(win32con.VK_SNAPSHOT, 1)
im = ImageGrab.grabclipboard()
im.save("screenshot.jpg", "JPEG")
Now wasn’t that easy!

