How to Take a Screenshot using Python

You may use the following template to take a screenshot using Python:

import pyautogui

my_screenshot = pyautogui.screenshot()
my_screenshot.save(r"Path to save screenshot\file name.png")

Steps to Take a Screenshot using Python

Step 1: Install the pyautogui package

To start, install the pyautogui package using this command:

pip install pyautogui

You may check the following guide for the instructions to install a package using PIP.

Step 2: Capture the path to save the screenshot

Next, capture the full path where the screenshot will be saved on your computer.

Here is an example of a path where the screenshot will be saved:

C:\Users\Ron\Desktop\Test\screenshot_1.png

Where, the file name is screenshot_1, and the file type is png (alternatively, you can set the file type to jpg).

Step 3: Take the screenshot using Python

For the final step, use the template below in order to take the screenshot:

import pyautogui

my_screenshot = pyautogui.screenshot()
my_screenshot.save(r"Path to save screenshot\file name.png")

For our example:

import pyautogui

my_screenshot = pyautogui.screenshot()
my_screenshot.save(r"C:\Users\Ron\Desktop\Test\screenshot_1.png")

Run the code in Python (adjusted to your path), and the screenshot will saved at your specified location.

Leave a Comment