Taking Screenshots with Python

Published by StudyMuch on

Taking Screenshot with Python:
Capturing screenshots is a common practice in various fields, from software development to data analysis. If you’re a Python learner and want to automate and improve your screenshot taking process, you’ve come to the right place. In this blog post, we will explore Taking Screenshots with Python, providing detailed explanations and code snippets.


Before diving into the code, let’s ensure you have the necessary tools installed. We’ll be using the PyAutoGUI Library for taking screenshots, which you can install using:

pip install pyautogui

Taking Screenshot with Python

Basic Screenshot with PyAutoGUI:

Let’s start with a simple example of taking a screenshot using the pyautogui library. The code below captures the entire screen and saves it as an image file:

import pyautogui
# Take a screenshot
screenshot = pyautogui.screenshot()

# Save the screenshot


  • We import the pyautogui library.
  • The pyautogui.screenshot() function captures the entire screen and returns an Image object.
  • We save the screenshot using the save() method, specifying the desired file name and format (in this case, a PNG image).

Capture Specific Region:

If you want to capture only a specific region of the screen, you can use the pyautogui.screenshot(region=(x, y, width, height)) function. Here’s an example:

import pyautogui
# Define the region to capture (x, y, width, height)
region = (100, 100, 800, 600)

# Take a screenshot of the specified region
screenshot = pyautogui.screenshot(region=region)

# Save the region screenshot


  • We define the region to capture by specifying the top-left coordinates (x, y) and the dimensions (width, height).
  • The pyautogui.screenshot(region=region) function captures the specified region of the screen.

Delayed Screenshot:

In some scenarios, you may need to introduce a delay before taking a screenshot. This can be useful, for example, when capturing a specific state of an application after a button click. Use the pyautogui.sleep() function to add a delay:

import pyautogui
import time
# Delay for 5 seconds

# Take a screenshot
screenshot = pyautogui.screenshot()

# Save the screenshot


  • We import the time module to use the sleep() function for introducing a 5-second delay before taking the screenshot.

Taking Screenshot & Opening Automatically

If you want to take a screenshot and automatically open it using Python, you can achieve this by combining the pyautogui library for taking the screenshot and the PIL (Pillow) library for opening and displaying the image.

import pyautogui
from PIL import Image
import time

def take_screenshot():
    screenshot= pyautogui.screenshot()
    print("Screenshot is taken and save")

    # Open the screenshot using PIL
    image = Image.open("screenshot.png")

    # Display the screenshot

if __name__ == "__main__":     


  • The Image.open() function from the PIL (Pillow) library is used to open the saved image.
  • The show() method displays the opened image using the default image viewer associated with your operating system.


Capturing screenshots with Python is a valuable skill that can streamline various tasks and enhance your workflow. The Pytogui library provides a simple yet powerful interface to automate the process of taking screenshots. Armed with the knowledge and examples provided in this blog post, you can confidently incorporate screenshot functionality into your Python projects. Experiment, explore, and upgrade your coding experience by mastering the art of taking screenshots with Python. Happy coding!

Learn More;


Leave a Reply

Avatar placeholder

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