Python Take Screenshot with Pillow: A Complete Guide for Beginners – Python Pillow Tutorial

By | August 30, 2020

Python can allow us to take screenshot easily. In this tutorial, we will introduce using python pillow library to take a screenshot for python beginners.

Preliminary

from PIL import ImageGrab

We will use ImageGrab model to take a screenshot.

Create a region you want to take

bbox = (300, 300, 550, 550)

In this tutorial, we will take a image 250 * 250.

In order to set bbox correctly, you can read this tutorial.

Understand Python Pillow bbox for Beginners – Python Pillow Tutorial

Take screenshot

im = ImageGrab.grab(bbox)

We will use ImageGrab.grab() function to get a snapshot of the screen.

Save snapshot to a file

im.save('screenshot.png')
im.close()

After getting a screenshot, we will save this image into a file. Open this file, we will see the snapshot is:

python take screenshot with pillow

Meanwhile, if you want to get a snapshot of full screen, you have two methods:

1.Set bbox = None, here is an example:

bbox = None
im = ImageGrab.grab(bbox)

Then you will get full screenshot of computer.

2.Get screen width and height

How to get screen width and height, you can read this tutorial.

Python Get Computer Screen Size Using PyAutoGUI: A Beginner Guide – Python Tutorial

Then you can do like this:

bbox = (0, 0, screen_widht, screen_height)
im = ImageGrab.grab(bbox)

Leave a Reply

Your email address will not be published. Required fields are marked *