Say, you run a Python program on your Windows machine and you want it to take a screenshot. How to accomplish this programmatically?
Method 1: Multiple Screen Shots Module
To programmatically record one or more screenshots in your Python program, run the
sct.shot() function from the
- Install the Multiple Screen Shots (MSS) module, e.g., by running
pip install mssin your Powershell or command line.
- Import the
mss()function from the
mssmodule using the Python statement:
from mss import mss
- Open a with environment using the Python statement:
with mss() as sct: ...
- Call the
shot()function on the
from mss import mss with mss() as sct: sct.shot()
If I run this on my Windows machine, it’ll create a file
"monitor-1.png" in the directory of the Python code file. Here’s how the screenshot file looks for me:
As you see from the last error message, the
mss module is not included in the standard library, so make sure to install it using pip or any other means to add the module to your environment.
You can find further examples on how to use the module here, for example if you need to customize the file path or create multiple screenshots.
Method 2: PIL.ImageGrab.grab()
A simple way to record screenshots on your Windows machine is to use the PIL module that is already installed in many environments.
- Install PIL
ImageGrabmodule using the Python statement:
- Create the screenshot and store it in a variable like so:
screenshot = PIL.ImageGrab.grab()
- Display the screenshot on your Windows machine:
Here’s the simple 3-liner code:
import PIL.ImageGrab screenshot = PIL.ImageGrab.grab() screenshot.show()
And here’s the screenshot popping up as a new window on my machine:
If you want to store the screenshot in a specific file location rather than displaying it with your standard image viewing program, use the following approach with
from PIL import ImageGrab screenshot = ImageGrab.grab() path = "C:\\Users\\MyUser\\Desktop\\MyScreenshot.jpg" screenshot.save(path)
Method 3: PyAutoGUI
Famous Python developer Al Sweighart has created a powerful library PyAutoGUI to control the mouse and keyboard and automate interactions with other applications. This approach works for Windows, macOS, and Linux, and Python 2 and 3. It also provides a function
pyautogui.screenshot(path) that stores a screenshot at a given path location on your computer.
To install PyAutoGUI, run
pip install pyautogui. Then run the following two-liner:
import pyautogui screenshot = pyautogui.screenshot('filename.png')
This is a Pythonic, simple, and concise way to create a screenshot in Python.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.