Python Screenshot Tutorial – How To Take Screenshot Using Python

Hey guys, taking screenshot is necessary for most of the application so in this post you will learn how to take screenshot using python. So let’s start Python Screenshot Tutorial.

There are various ways to take screenshot using python. The first and most popular way is using PyAutoGUI module. You can also use pillow module for taking screenshots in python. And you will also learn here, how to take screenshots in tkinter application. So let’s move towards our main topic.

Python Screenshot Tutorial – Getting Started

Taking Screenshot Using PyAutoGUI

PyAutoGUI is a cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.

Installing PyAutoGUI

To install PyAutoGUI module, you have to run following code on your command prompt.

Code For Screenshot Using PyAutoGUI

  • screenshot( ) method of pyautogui class is used to take screenshot of your screen.

Now write the following code on your python IDE.

  • First of all import pyautogui module.
  • Then create a variable(file) that will store the screenshot.
  • screenshot( ) method will take screenshot of your screen.
  • Now save this image by calling save( ) method. You have to pass name of screenshot to the save( ) function.

Let’s check the output –

Python Screenshot
Python Screenshot
  • Now you can see, the screenshot of your screen has been captured.

Taking Screenshot With Time

In the above example, you have seen that the screenshot of current screen is captured, but if you want to take screenshot of another screen such as your desktop or anything else, so what can you do. For this you have to use time module. sleep( ) method of time module is used to add delay in the execution of a program.

So now write the following program.

  • Firstly import time module.
  • Then call sleep( ) method and pass an argument which is your delay time. Here i am passing 6, you can pass as much you want.
  • Now run the code and go to desktop or wherever you want to take screenshot. After 6 second it will take screenshot of your screen.

Now lets check the output.

Python Screenshot
Python Screenshot

Taking Screenshot Using Pillow

  • Pillow is a Python Imaging Library (Fork).
  • The Python Imaging Library adds image processing capabilities to your Python interpreter.
  • This library provides extensive file format support, an efficient internal representation, and fairly powerful image processing capabilities.

Installing Pillow

Now run the following command on to your command prompt.

Code Snippet

Now you will see how to take screenshot using pillow module. So let’s start.

Pillow has a ImageGrab class which is used to copy the contents of the screen. grab( ) method is used to take screen shot.

Write the following code snippets.

  • bbox is the region which is to be copied.
  • bbox is consist of tuple. It has four values – the first one is value of x, second one is value of y, third one is value of width and fourth one is value of height.
  • The pixels inside the bounding box are returned as an “RGB” image on Windows or “RGBA” on OS X.
  • If bbox is not passed then the entire screen  will be captured.

Now lets see the output.

Python Screenshot
Python Screenshot

Taking Screenshot Using Keyboard

In this section you will see how to take screen shot by pressing key from keyboard.  Keyboard is a small Python library which can hook global events, register hotkeys, simulate key presses and much more.

Installing Keyboard Module

Run the following command to install keyboard module.

Code Snippet

Now write the following code to achieve the above task.

  • In this example i have passed p key as an argument to is_pressed( ) method. That means whenever you will enter p key from keyboard, your current screen will be captured.
  • Now run the code and go to the place wherever you want and now just press p key from keyboard. It will take the screenshot of your screen.

Now lets check the output. Here i have opened start option and press the p key from keyboard. So it has taken following output.

Python Screenshot
Python Screenshot

Taking Screenshot Using An Tkinter application

Now you will learn how to take screenshot from a Tkinter application.

So write the following code snippets on your editor.

Now run the above code. Now a GUI window will be appeared, now press the button present on this window. Your screen has been captured. Let’s see the output.

Python Screenshot
Python Screenshot

Conclusion

So guys, you have learned many ways to take screenshot using python. PyAutoGUI and Pillow is the two most important python library to take screenshot in python. It is very easy to implement. I hope you have learned a lot of valuable thing in Python Screenshot tutorial. If you have any query then ask here, i will try to short out your problems. And stay tuned with SIMPLIFIED PYTHON for latest and important tutorials. Thanks Everyone.

2 thoughts on “Python Screenshot Tutorial – How To Take Screenshot Using Python”

Leave a Comment