Python - Saving Screenshot



Description

A screenshot or screencapture is a process of capturing the image of computer desktop or anything shown on the screen to a image format file.In one single shot, everything that is one the desktop can be displayed.

Python with Image Library supports capturing the screenshot to any kind of image format.

For saving the image to pdf we need to import Image,ImageGrab and time module .

Image module :Image is the fundamental module for loading and opening the image and ImageGrab module is useful for taking the screenshot of the screen.
ImageGrab module :ImageGrab is used to grab the screens at the defined time frame
time module :Time module is used to capture current date and time or the previous.

Syntax

Following is the syntax of functions been used.
We use grab method from ImageGrab library to capture the screen
# Import Pillow:
from PIL import Image
from PIL import ImageGrab
im = ImageGrab.grab()

Parameters

From the ImageGrab library we use grab method to capture the screenshot in the timeframe of 10 milliseconds.

Note:If image is in same directory where your python program resides, you can give the image name itself. If the image resides in some other directory, you are supposed to define the complete path of the image.

Return value

When the program gets executed, program captures the screenshot after 10 milliseconds and stores in the local directory.

Example

The following example shows how to capture the screenshot and store it as image.

from PIL import Image
from PIL import ImageGrab
import time

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


Advertisements