Capture Your Code: A Beginner's Guide to Taking Screenshots in Python

Capture Your Code: A Beginner’s Guide to Taking Screenshots in Python

You can take a screenshot with Python. Python has a library called pyautogui, which is an automation tool. Install the library using pip:

pip install pyautogui

Below, we use pyautogui to take the screenshot. We then save the image and convert it from RGB to BGR using cv2 and numpy. We convert the image so it can be read by the cv2
library. Run this code below to see the magic. You can install the other libraries using:

Pip install numpy
pip install opencv-python

import pyautogui
import numpy as np
import cv2 as cv #Taking screenshot
image = pyautogui.screenshot()
image.save('my_screenshot.png')

#
convert RGB 2 BGR
image = cv.cvtColor(np.array(image),
cv.COLOR_RGB2BGR)
cv.imshow("image", image)
cv.waitKey(0)
cv.destroyAllWindows()
Scroll to Top
www.thecyberblogs.com