Python Print Screen and Save Image in 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!

This entry was posted in Python, Windows and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">