Skip to content
Related Articles

Related Articles

Improve Article

How to take screenshots using python?

  • Last Updated : 17 May, 2021

Python is a widely used general-purpose language. It allows performing a variety of tasks. One of them can be taking a screenshot. It provides a module named pyautogui which can be used to take the screenshot. This module along with NumPy and OpenCV provides the way to manipulate and save the images (screenshot in this case)

pyautogui takes pictures as a PIL(python image library) which supports opening, manipulating, and saving many different image file formats. Then we need to convert the image to NumPy array, so that it can be converted from RGB to BGR because when the image file is read with the OpenCV using imread(), the order of colors should be BGR (blue, green, red).

Modules needed

  • Numpy: To install Numpy type the below command in the terminal.
    pip install numpy
    
  • pyautogui: To install pyautogui type the below command in the terminal.
    pip install pyautogui
    
  • OpenCV: To install OpenCV type the below command in the terminal.
    pip install opencv-python
    

Below is the implementation.




# Python program to take
# screenshots
  
  
import numpy as np
import cv2
import pyautogui
   
  
# take screenshot using pyautogui
image = pyautogui.screenshot()
   
# since the pyautogui takes as a 
# PIL(pillow) and in RGB we need to 
# convert it to numpy array and BGR 
# so we can write it to the disk
image = cv2.cvtColor(np.array(image),
                     cv2.COLOR_RGB2BGR)
   
# writing it to the disk using opencv
cv2.imwrite("image1.png", image)

Output:



python-screenshot

 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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :