-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreenshoot_windows.py
218 lines (186 loc) · 6.92 KB
/
screenshoot_windows.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import os, sys, importlib
if importlib.util.find_spec('win32') is None:
sys.exit(
'\n'
f'{os.path.basename(__file__)} requires win32api and win32gui to work.\n'
'Please install with "pip install pywin32 --user"'
'\n'
)
import threading
from win32api import GetKeyState
from win32gui import GetCursorPos
import time
import pyautogui
VERSION=(0,0,1,1)
AUTHOR='Dominik Jarocki'
#Setup
shootTimeout = 1 #minimal time in seconds between shoots
screenshotPath = "./screenshots" #path which screenshots will be saved to
#End Of Setup
#WILL NOT TOUCH UNLESS YOUR IS CRAZY AND WANNA BRAKE SCRIPT AND CREATOR'S HEART
#-----------------------------------------------------------------------------#
version = ""
processEnabled = True
processTerminationFlag = 0
# 0-terminated 1-path not resolved
# 2-files exist in directory
# 3-region too small
# 4-cant set region to second screen
screenshotIndex = 0
screenshotNameTimestamp = True
screenRegion = (0, 0, 0, 0)
useCustomRegion = False
def y_prompt(prompt):
ans = str(input(str(prompt) + " (Y/n) "))
if ans.lower() == 'y'.lower() or ans.lower() == 'yes'.lower:
return True
return False
def checkPath(file_patch):
try:
if os.path.exists(file_patch) == False:
try:
os.mkdir(file_patch)
print("Path " + file_patch + " was created.")
except OSError:
print("Could not create directory.")
print("Screenshots will be saved to " + file_patch + ".\n")
except:
return
def checkForFiles(file_patch):
global processEnabled
if os.path.exists(file_patch + "/0.png"):
if not screenshotNameTimestamp:
print("\nScreenschots detected in directory. Continuing will result in overwriting existing files.")
ans = str(input("Continue? (Y/n) "))
if ans.lower() != 'y'.lower() and ans.lower() != 'yes'.lower:
#if y_prompt('Continue?') == False:
processEnabled = False
def init_screenshot():
th = threading.Thread(target=take_screenshot)
th.start()
def take_screenshot():
global screenshotIndex
timestamp = str(screenshotIndex)
if screenshotNameTimestamp:
timestamp = time.strftime('%d%m%y%H%M%S', time.gmtime())
try:
file_path = screenshotPath + "/" + timestamp + ".png"
myScr = pyautogui.screenshot(region=screenRegion)
if useCustomRegion:
myScr = pyautogui.screenshot(region=screenRegion)
else:
myScr = pyautogui.screenshot()
myScr.save(file_path)
screenshotIndex += 1
print(f'Screenshot saved to: ' + file_path)
except:
print("Due to some extreme exception screenshot attempt failed spectacularly!")
#Key Detection Functions
def key_down(key):
state = GetKeyState(key)
if (state != 0) and (state != 1):
return True
else:
return False
def key_detect():
if key_down(0x11) and key_down(0x12):
if key_down(0x50):
init_screenshot()
time.sleep(shootTimeout)
#elif key_down(0x51):
#processEnabled = False
def prompt_region():
if y_prompt("Capture whole screen?") == False:
setup_region()
def setup_region():
global screenRegion
global useCustomRegion
global processEnabled
global processTerminationFlag
x1 = 0
y1 = 0
x2 = 0
y2 = 0
print("\nMove cursor over primary (top-left) point and press CTR+ALT+P.")
while key_down(0x11) == False or key_down(0x12) == False or key_down(0x50) == False:
continue
(x1,y1) = GetCursorPos()
print("Primary point set at (" + str(x1) + "," + str(y1) + ").")
time.sleep(0.5)
print("\nMove cursor over secondary (left-top) point and press CTR+ALT+P.")
while key_down(0x11) == False or key_down(0x12) == False or key_down(0x50) == False:
continue
(x2,y2) = GetCursorPos()
print("Primary point set at (" + str(x2) + "," + str(y2) + ").")
time.sleep(0.5)
if x2<=x1 or y2<=y1:
print("Screen area must be larger than 0!")
processEnabled = False
processTerminationFlag = 3
screenRegion = (x1,y1,x2-x1,y2-y1)
useCustomRegion = True
def process_intro():
print("\nWelcome to simple python screen shooter by DominJgon\n\n")
print(f'Type "{os.path.basename(__file__)} --help" to see manual.')
print("In order to change settings head into file section #setup.")
#print("Before using make sure previous screenshots were saved outside of directory\n")
print("Screen shooter currently supports only primary display.")
time.sleep(1)
def process_ready():
print("\nCTR+ALT+P to take screenshot, CTR+PAUSE quit.\n")
def main():
process_intro()
checkPath(screenshotPath)
checkForFiles(screenshotPath)
if processEnabled == False:
print("\nIn order to use script please remove files or allow overwriting.\n")
time.sleep(1)
return
prompt_region()
if processEnabled == False:
print("\nTerminating")
time.sleep(1)
return
process_ready()
while processEnabled:
key_detect()
print("\n\nThank you for using my script.")
def terminate():
return
def handleArgs():
global processEnabled
global screenshotNameTimestamp
if len(sys.argv) > 0:
if '-h' in sys.argv or '--help' in sys.argv:
print(
'\n'
'This is a screenshot taking script for quick shortcut use.\n'
'Screenshot is taken by pressing Ctrl + Alt + P at any time\n'
'the script is running.\n'
'By default taken screenshots are saved in ./screenshots directory.\n'
'\n'
f'Usage: {os.path.basename(__file__)} [options]\n'
' -h, --help -Shows this help and quits.\n'
' -v, --version -Shows version of script.\n'
' -nt --no-time -Disables timestamp forcing script into\n'
' overwrite mode. Saving screenshots will began from 0,\n'
' and reset every time script will be launched.\n'
, end='\n')
sys.exit()
elif '-v' in sys.argv or '--version' in sys.argv:
print(f'Version: {".".join(str(_) for _ in VERSION)}\nAuthor: {AUTHOR}')
sys.exit()
elif '-nt' in sys.argv or '--no-time' in sys.argv:
screenshotNameTimestamp = False
return
elif '--raise-test-error' in sys.argv:
raise NameError('TestError')
return
else:
return
try:
handleArgs()
main()
terminate()
except (KeyboardInterrupt, SystemExit):
pass