You may use the following template to take a screenshot using Python:
import pyautogui myScreenshot = pyautogui.screenshot() myScreenshot.save(r'Path to save screenshot\file name.png')
In the next section, you’ll see the complete steps to take a screenshot using Python.
Steps to Take a Screenshot using Python
Step 1: Install the pyautogui package
To start, you’ll need to install the pyautogui package using the following command (under Windows):
pip install pyautogui
You may check the following guide for the instructions to install a package using pip for Windows users.
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 (note that you’ll need to specify your own path when running the code below):
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, you’ll need to use the template below in order to take the screenshot using Python:
import pyautogui myScreenshot = pyautogui.screenshot() myScreenshot.save(r'Path to save screenshot\file name.png')
Here is the code to take the screenshot for our example (as indicated earlier, you’ll need to change the path to reflect the location where the screenshot will be saved on your computer):
import pyautogui myScreenshot = pyautogui.screenshot() myScreenshot.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.