Understand Python Pillow ImageGrab Module: A Beginner Guide – Python Pillow Tutorial

By | December 1, 2020

Python pollow ImageGrab module can be used to copy the contents of the screen or the clipboard to a PIL image memory. In this tutorial, we will introduce how to use it.

There are two methods in ImageGrab.

PIL.ImageGrab.grab(bbox=None, include_layered_windows=False, all_screens=False, xdisplay=None)

ImageGrab.grab() method will take a snapshot of the screen.

Parameter

bbox – What region to copy. Default is the entire screen.

How to create a bbox? You can read:

Understand Python Pillow bbox for Beginners – Python Pillow Tutorial

PIL.ImageGrab.grabclipboard()

It will take a snapshot of the clipboard image.

We will use an example to show you how to use ImageGrab.grab().

How to use ImageGrab.grab()?

Look at example code below:

from PIL import ImageGrab
 
curScreen = ImageGrab.grab()

print(type(curScreen))
print(curScreen)

This code will take a snapshot of the screen.

Run this code, you can get this result.

<class 'PIL.Image.Image'>
<PIL.Image.Image image mode=RGB size=1366x768 at 0x1A436543220>

From this result, we can find:

ImageGrab.grab() will return a PIL.Image.Image object, you can use python pillow Image model to process it.

Meanwhile, if you want to convert this Image object to numpy, you can refer:

Python Pillow Read Image to NumPy Array: A Step Guide – Python Pillow Tutorial

Leave a Reply