Taking screenshots with Python

Hello everyone, in this tutorial, we are going to learn how to take screenshots with Python. There are more than one ways to take a screenshot using Python. We will discuss some of these methods in this post.

Taking a screenshot using pyautogui

Python provides us with a number of useful modules. One such module is pyautogui. We will be using this module to take a screenshot of our screen. It uses screenshot() method to take the screenshot of the whole screen. To install this, we can use the following command in the command prompt.

pip install pyautogui

See the following code and the output.

import pyautogui

im = pyautogui.screenshot()


The above code takes the screenshot of the screen and saves the file with the name ‘im1.png’ as shown in the code. Here a sample of the output.

We can also use the time module here to take a screenshot after waiting for some time. Have a look at the following code.

import pyautogui
import time


im = pyautogui.screenshot()


The above program waits for 6 seconds and then take the screenshot and saves it as ‘im2.png’.

Taking a screenshot using pillow

Another module in Python to take a screenshot is pillow. Type the following command in the command prompt to install this.

pip install pillow

This module has an ImageGrab class that uses the grab() method to take the screenshot. This method takes an input that defines the region for the screenshot. See the below code.

from PIL import ImageGrab

region = (100, 100, 500, 500)

im = ImageGrab.grab(region)



We can use time.sleep() method to wait for some time before taking the screenshot.

Also read: How to assign a value to a TensorFlow variable in Python

Leave a Reply

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