Capturing the screenshot using python


Need to install pyautogui first by using pip install pyautogui command

import pyautogui

def capture_screenshot(filename="screenshot.png"):

try:
    screenshot = pyautogui.screenshot()
    screenshot.save(filename)
    print(f"Screenshot saved as {filename}")
except Exception as e:
    print(f"Error capturing screenshot: {e}")

if name == "main":
capture_screenshot() # Captures and saves as screenshot.png
#capture_screenshot("my_capture.jpg") #example of changing filename