import time
import mss
import cv2
import uuid
import numpy as np
import ctypes
import ctypes.wintypes
import os
keybind = '0xBE'
image_size = 640
delay_between_screenshots = 1
images_folder_path = 'images'
half_screen_x = int(ctypes.windll.user32.GetSystemMetrics(0) / 2)
half_screen_y = int(ctypes.windll.user32.GetSystemMetrics(1) / 2)
camera = mss.mss()
pause = number_of_images_taken = 0
box = {
'left': int(half_screen_x - image_size//2),
'top': int(half_screen_y - image_size//2),
'width': int(image_size),
'height': int(image_size)
}
if not os.path.exists(images_folder_path):
print('Creating images folder..')
os.mkdir('images')
def is_holding_keydind():
user32 = ctypes.windll.user32
return user32.GetAsyncKeyState(int(keybind, 16)) & 0x8000
while True:
frame = np.array(camera.grab(box))
if frame is not None:
if is_holding_keydind() and time.perf_counter() - pause > 1:
frame_copy = np.copy(frame)
cv2.imwrite(f'images/{str(uuid.uuid4())}.jpg', frame_copy)
pause = time.perf_counter()
number_of_images_taken += 1
print(f'Screenshots taken: {number_of_images_taken}')