2 Ways to Take Screenshots in Python

Learn how to take screenshots in Python using two easy methods: the pyautogui and Pillow modules. With these modules, you can take screenshots of your entire screen, a specific region of your screen, or even individual windows. Whether you're a beginner or a seasoned pro, this article will teach you everything you need to know about taking screenshots in Python.

📙 20 Best Python Books for Beginners and Experienced Coders

How to capture screenshots using Python

Python offers various libraries to capture screenshots. We’ll be exploring a few of these libraries today and understand how you can implement the code in Python to capture your screens.

Method 1: Using pyautogui module

The pyautogui module makes use of the screenshot function which is responsible for taking the screenshot of the whole computer screen. And then the save function is used to save the screenshot captured to our device.

import pyautogui
im = pyautogui.screenshot()
im.save("SS1.jpg")

The image saved would look something like this.

capture screenshots

Screenshot Python 1

If one wants some delay before taking an automated screenshot, the programmer can make use of the time module and making use of the sleep function.

Method 2: Using pillow module

The pillow module makes use of an ImageGrab submodule. This method requires a region that needs to be captured which implies setting the diagonal coordinates of the region.

Then we make use of the grab function which will take the region parameters to capture the screenshot. And finally, save the captured image using the save function.

from PIL import ImageGrab
ss_region = (300, 300, 600, 600)
ss_img = ImageGrab.grab(ss_region)
ss_img.save("SS3.jpg")

The region captured is shown below. We can also make use of the time module to delay the capturing of the screenshot.

capture screenshots

Screenshot Python 2

Conclusion

So now you know two methods to capture screenshots of your computer screen. And yes there are other methods as well as python is a very advanced language. Hope you liked reading it!

Thank you for reading!

#python

2 Ways to Take Screenshots in Python
7.40 GEEK