| Python Imaging Library Handbook |
The ImageGrab module can be used to copy the contents of the screen or the clipboard to a PIL image memory.
The current version works on Windows only.
ImageGrab.grab() => image
ImageGrab.grab(bbox) => image
(New in 1.1.3) Take a snapshot of the screen, and return an "RGB" image. The bounding box argument can be used to copy only a part of the screen.
ImageGrab.grabclipboard() => image or list of strings or None
(New in 1.1.4) Take a snapshot of the clipboard contents, and return an image object or a list of file names. If the clipboard doesn't contain image data, this function returns None.
You can use isinstance to check if the function returned a valid image object, or something else:
im = ImageGrab.grabclipboard()
if isinstance(im, Image.Image):
... got an image ...
elif im:
for filename in im:
try:
im = Image.open(filename)
except IOError:
pass # ignore this file
else:
... got an image ...
else:
... clipboard empty ...