Taking Screenshots using pyscreenshot in Python

Python offers multiple libraries to ease our work. Here we will learn how to take a screenshot using Python. Python provides a module called pyscreenshot for this task. It is only a pure Python wrapper, a thin layer over existing backends. Performance and interactivity are not important for this library.

Installation

Install the package pyscreenshot using the below command in your command prompt.

pip install pyscreenshot

Capturing Full Screen

Here we will learn the simplest way of taking a screenshot using pyscreenshot module. Here we will use the function show() to view the screenshot. 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Program to take screenshot
  
import pyscreenshot
  
# To capture the screen
image = pyscreenshot.grab()
  
# To display the captured screenshot
image.show()
  
# To save the screenshot
image.save("GeeksforGeeks.png")

chevron_right


Output:



Full Screenshot

Capturing part of the screen

Here is the simple Python program to capture the part of the screen. Here we need to provide the pixel positions in the grab() function. We need to pass the coordinates in the form of a tuple. 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Program for partial screenshot
  
import pyscreenshot
  
# im=pyscreenshot.grab(bbox=(x1,x2,y1,y2))
image = pyscreenshot.grab(bbox=(10, 10, 500, 500))
  
# To view the screenshot
image.show()
  
# To save the screenshot
image.save("GeeksforGeeks.png")

chevron_right


Output:

Partial Screenshot

Important Points:

  • We need to install pillow (PIL) package before installing pyscreenshot package.
  • Here show() function works as print i.e. It displays the captured screenshot.
  • We need to pass the coordinates in tuple.
  • We can save the screenshot to a file or PIL image memory.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.