[Tutor] Taking a screenshot

Magnus Lycka magnus@thinkware.se
Tue Jan 7 17:03:21 2003


At 18:37 2003-01-06 +0000, alan.gauld@bt.com wrote:
>It might be possible to set up a 3rd party tool to do so using os.system()

This is certainly possible.

For Windows, the win32all bundle has a win32clipboard module,
but the GetClipboardData function doesn't seem to handle images,
only text, so that is of little help. (Maybe there is another way
here.) Otherwise I guess it could have been possible to use some
other function that triggered PrintScreen which would put a screenshot
in the clipboard.

Actually, there is one case where I think it's fairly simple to
get screenshots in Python--cross platform even. Tkinter canvases
can dump screenshots if I understand correctly. At least as PostScript.
But that's no help if you want to get some other kind of screenshot...

Hang on, isn't there support for this in PIL? Yes, if you are using
Windows, it's supported in PIL from version 1.1.3. See the ImageGrab
module in http://www.pythonware.com/products/pil/pil-handbook.pdf

It doesn't seem to work quite as the docs suggests though... I
couldn't find any ImageGrab module. Ask on comp.lang.python, and
I'm pretty sure Fredrik will respond. Else, use his older, separate
grabscreen from http://effbot.org/downloads/

With PIL and grabscreen, I tried this, and it worked:

from PIL import Image
import _grabscreen
size, data = _grabscreen.grab()
im = Image.fromstring("RGB", size, data, "raw", "BGR", (size[0]*3+3) & -4, -1)
im.show()


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se