take screenshot in windows using python code example

Example 1: screenshot in python

import pyautogui


class gng() :
    myScreenshot = pyautogui.screenshot()
    myScreenshot.save('C:\File\Code\save.png')


gng()

Example 2: python screenshot specific window

import pyautogui
import win32gui

def screenshot(window_title=None):
    if window_title:
        hwnd = win32gui.FindWindow(None, window_title)
        if hwnd:
            win32gui.SetForegroundWindow(hwnd)
            x, y, x1, y1 = win32gui.GetClientRect(hwnd)
            x, y = win32gui.ClientToScreen(hwnd, (x, y))
            x1, y1 = win32gui.ClientToScreen(hwnd, (x1 - x, y1 - y))
            im = pyautogui.screenshot(region=(x, y, x1, y1))
            return im
        else:
            print('Window not found!')
    else:
        im = pyautogui.screenshot()
        return im


im = screenshot('Calculator')
if im:
    im.show()