# Done by Frannecklp

import cv2
import numpy as np
import win32gui, win32ui, win32con, win32api
import matplotlib.pyplot as plt
import pickle
import os,shutil

box = (45,37,912,511)
def grab_screen(region=box):

    hwin = win32gui.GetDesktopWindow()

    if region:
            left,top,x2,y2 = region
            width = x2 - left + 1
            height = y2 - top + 1
    else:
        width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
        height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
        left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
        top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)

    hwindc = win32gui.GetWindowDC(hwin)
    srcdc = win32ui.CreateDCFromHandle(hwindc)
    memdc = srcdc.CreateCompatibleDC()
    bmp = win32ui.CreateBitmap()
    bmp.CreateCompatibleBitmap(srcdc, width, height)
    memdc.SelectObject(bmp)
    memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)
    
    signedIntsArray = bmp.GetBitmapBits(True)
    img = np.fromstring(signedIntsArray, dtype='uint8')
    img.shape = (height,width,4)

    srcdc.DeleteDC()
    memdc.DeleteDC()
    win32gui.ReleaseDC(hwin, hwindc)
    win32gui.DeleteObject(bmp.GetHandle())

    return cv2.cvtColor(img, cv2.COLOR_BGRA2RGB)

def im_show(img):
    plt.figure()
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    plt.imshow(img)
    
def cv2_show(window,img,pos=(0,384+30)):
    img = cv2.resize(img,(683,384),interpolation=cv2.INTER_NEAREST)
    cv2.namedWindow(window)        
    cv2.moveWindow(window, pos[0],pos[1])
    cv2.imshow(window,img)
    cv2.waitKey(25)

def load_pickle(file):
    with open(file,'rb') as f:
        return pickle.load(f)
    
def save_pickle(x,path):
    with open(path,'wb') as f:
        pickle.dump(x,f)
        
def remove_content(folder):
    for file in os.listdir(folder):
        file_path = os.path.join(folder, file)
        if os.path.isfile(file_path):
            os.unlink(file_path)
        elif os.path.isdir(file_path): 
            shutil.rmtree(file_path) #remove directories in the folder


     


# Done by Frannecklp

import cv2
import numpy as np
import win32gui, win32ui, win32con, win32api
import matplotlib.pyplot as plt
import pickle
import os,shutil

box = (45,37,912,511)
def grab_screen(region=box):

    hwin = win32gui.GetDesktopWindow()

    if region:
            left,top,x2,y2 = region
            width = x2 - left + 1
            height = y2 - top + 1
    else:
        width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
        height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
        left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
        top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)

    hwindc = win32gui.GetWindowDC(hwin)
    srcdc = win32ui.CreateDCFromHandle(hwindc)
    memdc = srcdc.CreateCompatibleDC()
    bmp = win32ui.CreateBitmap()
    bmp.CreateCompatibleBitmap(srcdc, width, height)
    memdc.SelectObject(bmp)
    memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)
    
    signedIntsArray = bmp.GetBitmapBits(True)
    img = np.fromstring(signedIntsArray, dtype='uint8')
    img.shape = (height,width,4)

    srcdc.DeleteDC()
    memdc.DeleteDC()
    win32gui.ReleaseDC(hwin, hwindc)
    win32gui.DeleteObject(bmp.GetHandle())

    return cv2.cvtColor(img, cv2.COLOR_BGRA2RGB)

def im_show(img):
    plt.figure()
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    plt.imshow(img)
    
def cv2_show(window,img,pos=(0,384+30)):
    img = cv2.resize(img,(683,384),interpolation=cv2.INTER_NEAREST)
    cv2.namedWindow(window)        
    cv2.moveWindow(window, pos[0],pos[1])
    cv2.imshow(window,img)
    cv2.waitKey(25)

def load_pickle(file):
    with open(file,'rb') as f:
        return pickle.load(f)
    
def save_pickle(x,path):
    with open(path,'wb') as f:
        pickle.dump(x,f)
        
def remove_content(folder):
    for file in os.listdir(folder):
        file_path = os.path.join(folder, file)
        if os.path.isfile(file_path):
            os.unlink(file_path)
        elif os.path.isdir(file_path): 
            shutil.rmtree(file_path) #remove directories in the folder


     


# Done by Frannecklp #remove directories in the folder