How to Take a Screenshot using Python

In this tutorial, you will learn how to take screenshots using Python.

TLDR solution

screenshot.py
from PIL import ImageGrab

screenshot = ImageGrab.grab()

screenshot.save("/target-path/screenshot.png")

screenshot.close()

Step-by-Step Example

Step 1: Install the Pillow Package

If you don't have Pillow already installed, execute the following command in your terminal:

pip install pillow

Step 2: Screenshot the Entire Screen

You can Pillow's ImageGrab module to screenshot your screen and save the file to your desktop:

TLDR solution

screenshot.py
from PIL import ImageGrab
import os

screenshot = ImageGrab.grab()

desktop_path = os.path.expanduser("~/Desktop")
screenshot.save(desktop_path + "screenshot.png")

screenshot.close()

That's it! You just learned how to screenshot your screen using Python.