2 Ways to Capture Screenshots Using Python

Featured Img Screenshot

In this tutorial, we will learn how can one take screenshots using the Python programming language. There are multiple ways to achieve the same, we will be discussing some of them in the coming sections.

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!