-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcaptures.py
58 lines (43 loc) · 1.59 KB
/
captures.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import cv2
from PIL import ImageGrab
import numpy as np
import keyboard
import os
from datetime import datetime
current_key = ""
buffer = []
# check if folder named 'captures' exists. If not, create it.
if not os.path.exists("captures"):
os.mkdir("captures")
def keyboardCallBack(key: keyboard.KeyboardEvent):
'''
This function is called when a keyboard event occurs. It stores the key pressed in a buffer and sorts it.
### Arguments :
`key (KeyboardEvent)`
### Returns :
`None`
### Example :
`keyboardCallBack(key)`
'''
global current_key
if key.event_type == "down" and key.name not in buffer:
buffer.append(key.name)
if key.event_type == "up":
buffer.remove(key.name)
buffer.sort() # Arrange the keys pressed in an ascending order
current_key = " ".join(buffer)
keyboard.hook(callback=keyboardCallBack)
i = 0
while (not keyboard.is_pressed("esc")):
# Capture image and save to the 'captures' folder with time and date along with the key being pressed
image = cv2.cvtColor(np.array(ImageGrab.grab(
bbox=(390, 220, 1000, 400))), cv2.COLOR_RGB2BGR)
# if key pressed embed the key pressed in the file name
if len(buffer) != 0:
cv2.imwrite("captures/" + str(datetime.now()).replace("-", "_").replace(":",
"_").replace(" ", "_")+" " + current_key + ".png", image)
# if no key pressed embed 'n' in the file name
else:
cv2.imwrite("captures/" + str(datetime.now()).replace("-",
"_").replace(":", "_").replace(" ", "_") + " n" + ".png", image)
i = i+1