Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

How to take screenshots using python?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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

My Personal Notes arrow_drop_up
Last Updated : 03 Jan, 2023
Like Article
Save Article
Similar Reads
Related Tutorials