python capture screen 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 screen capture a window

from win32gui import FindWindow, GetWindowRect
from PIL import ImageGrab
from PIL import Image
import numpy as np
import cv2

while True:
    window_handle = FindWindow(None, "MTGA")
    window_rect = GetWindowRect(window_handle)
    screen = np.array(ImageGrab.grab(bbox=(window_rect)))
    resized = cv2.resize(screen, (1280, 720), interpolation = cv2.INTER_AREA)
    im_rgb = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB)
    cv2.imshow('Python Window', im_rgb)
    if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break