How to Take a Screenshot using Python

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')

Next, I’ll show you the full steps to take a screenshot using Python. I’ll also include the code to create a GUI to take screenshots:

GUI to take Screenshots

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:

pip install pyautogui

You may follow this source 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.

In my case, I decided to save the screenshot under the following path:

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

Here, the file name is screenshot1, while 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')

For our example, I applied the following code to take the screenshot (note that 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\screenshot1.png')

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

How to Take a Screenshot using Python

Tool to take screenshots using Python

Now, I’ll share with you the code to create a GUI that will allow you to take a screenshot by clicking a button.

As before, you’ll need to adjust the path to reflect the location where the screenshot will be saved on your computer:

import pyautogui
import tkinter as tk

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 300, height = 300)
canvas1.pack()

def takeScreenshot ():
    
    myScreenshot = pyautogui.screenshot()
    myScreenshot.save(r'C:\Users\Ron\Desktop\Test\screenshot2.png')

myButton = tk.Button(text='Take Screenshot', command=takeScreenshot, bg='green',fg='white',font= 10)
canvas1.create_window(150, 150, window=myButton)

root.mainloop()

Run the code, and you’ll see this GUI:

GUI to take Screenshots

Click on the button, and the screenshot will be saved at your specified location:

Take a Screenshot using Python

So far, you saw how to take a screenshot by specifying the path where the screenshot will be saved. But did you know that you could also take screenshots without specifying the path in the code?

You can definitely do that using the code below:

import pyautogui
import tkinter as tk
from tkinter import filedialog

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 300, height = 300)
canvas1.pack()

def takeScreenshot ():
    
    myScreenshot = pyautogui.screenshot()
    file_path = filedialog.asksaveasfilename(defaultextension='.png')
    myScreenshot.save(file_path)

myButton = tk.Button(text='Take Screenshot', command=takeScreenshot, bg='green',fg='white',font= 10)
canvas1.create_window(150, 150, window=myButton)

root.mainloop()

Run the code, and you’ll get the following GUI:

GUI to take Screenshots

Click on the button to take the screenshot, and you’ll see a dialogue box that will allow you to choose the location where the screenshot will be saved:

Save Screenshot