Python ImageGrab Module



ImageGrab module

The ImageGrab module is used to capture the image of computer desktop or anything shown on the screen to a image format file.Everything that is on the desktop can be displayed.

ImageGrab module has two methods : grab and grabclipboard . grab method is used to capture the entire screen and grabclipboard method is used to grab only the clipboard image.

If the file cannot be opened, an IOError exception is raised.

Importing Image module

from PIL import ImageGrab


Image module functions
ImageGrab.grab(bbox=None) Take a snapshot of the screen. The pixels inside the bounding box are returned as an RGB image. If the bounding box is omitted, the entire screen is copied.
ImageGrab.grabclipboard() Take a snapshot of the clipboard image, if any.


Example
from PIL import ImageGrab
import time

#Screenshot is captured after 10 milliseconds
time.sleep(10)
im = ImageGrab.grab()
# file name will be created with the current time frame
screenshot = time.strftime("%Y%m%d-%H%M%S")
screenshot = screenshot + ".jpg"
print screenshot
im.save(screenshot)
im.show()


Advertisements