How to take a screenshot via a Python script on Linux

Sometimes, we want to take a screenshot via a Python script on Linux.

In this article, we’ll look at how to take a screenshot via a Python script on Linux.

How to take a screenshot via a Python script on Linux

To take a screenshot via a Python script on Linux, we can use pyscreenshot.

To install it, we run

pip install Pillow pyscreenshot

Then we use it by writing

import pyscreenshot as ImageGrab

im = ImageGrab.grab()
im.show()

im = ImageGrab.grab(bbox=(10, 10, 500, 500))
im.show()

ImageGrab.grab_to_file('im.png')

to call grab to take a screenshot of the whole screen by calling it without arguments.

We call grab with the bbox argument to take the screenshot of part of the screen.

And we call grab_to_file to take a screenshot and save it to a file.

Conclusion

To take a screenshot via a Python script on Linux, we can use pyscreenshot.