import ctypes
import cv2
import time
import win32gui
import numpy as np
if __name__ == '__main__':
dxgi = ctypes.CDLL("G:\\1\\repo\\dxgi4py\\x64\\Debug\\dxgi4py.dll")
dxgi.grab.argtypes = (ctypes.POINTER(ctypes.c_ubyte), ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int)
dxgi.grab.restype = ctypes.POINTER(ctypes.c_ubyte)
# dxgi.grab.argtypes = (
# np.ctypeslib.ndpointer(dtype=np.uint8, ndim=3, flags='C_CONTIGUOUS'), ctypes.c_int, ctypes.c_int, ctypes.c_int,
# ctypes.c_int)
# dxgi.grab.restype = np.ctypeslib.ndpointer(dtype=np.uint8, ndim=3, flags='C_CONTIGUOUS')
hwnd = win32gui.FindWindow(None, "Ice.mp4 - PotPlayer")
if hwnd == 0:
raise Exception(f"Cannot find window with title")
dxgi.init_dxgi(hwnd)
left, top, right, bottom = win32gui.GetWindowRect(hwnd)
shotLeft, shotTop = 0, 0
height = bottom - top
width = right - left
imgs = []
for i in range(20):
shot = np.ndarray((height, width, 4), dtype=np.uint8)
shotPointer = shot.ctypes.data_as(ctypes.POINTER(ctypes.c_ubyte))
dxgi.grab(shotPointer, 0, 0, width, height)
# dxgi.grab(shot, 0, 0, width, height)
imgs.append(shot)
time.sleep(0.05)
for i in range(20):
cv2.imwrite(f'tempcap4/{i}.png', imgs[i])
cv2.waitKey(0)