-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scanpos-Helper.py
204 lines (177 loc) · 6.73 KB
/
Scanpos-Helper.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
from tkinter import StringVar, filedialog, messagebox
import tkinter as tk
from PIL import ImageTk, Image, ImageEnhance, ImageFilter, ImageGrab
import ctypes
from pynput import keyboard
from pynput import mouse
try: # Windows 8.1 and later
ctypes.windll.shcore.SetProcessDpiAwareness(2)
except Exception as e:
pass
def get_curr_screen_geometry():
"""
Workaround to get the size of the current screen in a multi-screen setup.
Returns:
geometry (str): The standard Tk geometry string.
[width]x[height]+[left]+[top]
"""
root = tk.Tk()
root.update_idletasks()
root.attributes('-fullscreen', True)
root.state('iconic')
#geometry = root.winfo_geometry()
geometry_x = root.winfo_width()
geometry_y = root.winfo_height()
#geometry_sx = root.winfo_screenwidth()
#geometry_sy = root.winfo_screenheight()
root.destroy()
return geometry_x, geometry_y
image_contrast_float = 1.0
filter_pre_enhance = True
remove_white = False
window_alpha = 0.5
screenwidth, screenheight = get_curr_screen_geometry()
window_size = {"width": 422, "height": 400}
window_pos = {
"posx": screenwidth - window_size["width"] - 20,
"posy": (screenheight - window_size["height"]) - 1000
}
window_geometry = "{0}x{1}+{2}+{3}".format(window_size["width"],
window_size["height"],
window_pos["posx"],
window_pos["posy"])
def prepareImage(filepath, contrast_float):
nxtimage = Image.open(filepath)
if filter_pre_enhance:
nxtimage = nxtimage.filter(ImageFilter.EDGE_ENHANCE_MORE)
enhancer = ImageEnhance.Contrast(nxtimage)
nxtimage = enhancer.enhance(contrast_float)
if not filter_pre_enhance:
nxtimage = nxtimage.filter(ImageFilter.EDGE_ENHANCE_MORE)
if remove_white:
limage = nxtimage.convert("L")
source = limage
mask = source.point(lambda i: i < 150 and i)
mask = mask.point(lambda i: i != 0 and 255)
nxtimage = mask
image = ImageTk.PhotoImage(nxtimage)
return image
def showLoadSaveDialog():
window = tk.Tk()
window.geometry(window_geometry)
window.attributes("-topmost", "true")
file_path_load = StringVar()
file_path_load.set("-")
file_path_save = StringVar()
file_path_save.set("-")
def filedialogopen():
file_path_load.set(filedialog.askopenfilename())
if (file_path_load.get() and len(file_path_load.get()) > 1):
window.destroy()
def filedialogsave():
window.attributes("-alpha", 0)
file_path_save.set(
filedialog.asksaveasfilename(defaultextension="bmp"))
if (file_path_save.get() and len(file_path_save.get()) > 1):
ss = ImageGrab.grab()
ss.save(file_path_save.get(), "bmp")
messagebox.showinfo("Fertig!", "Screenshot gespeichert!")
window.destroy()
else:
window.attributes("-alpha", 1)
def end():
window.destroy()
exit()
ipadding = {'ipadx': 10, 'ipady': 10, "padx": 0, "pady": 0}
tk.Label(
window,
text="Falls erste Messung eines Artefaktes:\nScreenshot aufnehmen!",
anchor=tk.W,
bg="white").pack(**ipadding,
fill=tk.X)
tk.Label(window,
text="Falls folgende Messung: Screenshot laden!",
anchor=tk.W,
bg="white").pack(**ipadding,
fill=tk.X)
tk.Button(window,
text='Screenshot laden!',
command=filedialogopen).pack(**ipadding,
fill=tk.X)
tk.Button(window,
text='Screenshot aufnehmen!',
command=filedialogsave).pack(**ipadding,
fill=tk.X)
tk.Button(window, text='Ende', command=end).pack(**ipadding, fill=tk.X)
window.mainloop()
return (file_path_load.get(), file_path_save.get())
"""
def get_curr_screen_geometry():
""" """
Workaround to get the size of the current screen in a multi-screen setup.
Returns:
geometry (str): The standard Tk geometry string.
[width]x[height]+[left]+[top]
""" """
root = Tk()
root.update_idletasks()
root.attributes('-fullscreen', True)
root.state('iconic')
geometry = root.winfo_geometry()
root.destroy()
return geometry
"""
def makeOverlayWindow(filepath):
def on_release(key):
global image_contrast_float, image, filter_pre_enhance, remove_white, window_alpha
print('{0} released'.format(key))
if key == keyboard.Key.esc:
window.deiconify()
window.destroy()
return False
if key == keyboard.Key.up:
image_contrast_float = image_contrast_float + 1.0
image = prepareImage(filepath, image_contrast_float)
canvas.itemconfig(image_container, image=image)
if key == keyboard.Key.down:
image_contrast_float = image_contrast_float - 1.0
image = prepareImage(filepath, image_contrast_float)
canvas.itemconfig(image_container, image=image)
if key == keyboard.Key.left:
filter_pre_enhance = not filter_pre_enhance
image = prepareImage(filepath, image_contrast_float)
canvas.itemconfig(image_container, image=image)
if key == keyboard.Key.right:
remove_white = not remove_white
image = prepareImage(filepath, image_contrast_float)
canvas.itemconfig(image_container, image=image)
if key == keyboard.Key.page_up:
window_alpha = window_alpha + 0.05
window.attributes("-alpha", window_alpha)
if key == keyboard.Key.page_down:
window_alpha = window_alpha - 0.05
window.attributes("-alpha", window_alpha)
# ...or, in a non-blocking fashion:"""
listener = keyboard.Listener(on_release=on_release)
listener.start()
#creating window
window = tk.Tk()
#getting screen width and height of display
width = window.winfo_screenwidth()
height = window.winfo_screenheight()
#setting tkinter window size
window.geometry("%dx%d" % (width, height))
window.geometry("+0+0")
window.overrideredirect(True)
window.attributes("-alpha", window_alpha)
window.attributes('-topmost', 'true')
canvas = tk.Canvas(window, width=width, height=height)
canvas.pack()
image = prepareImage(filepath, image_contrast_float)
image_container = canvas.create_image(0, 0, anchor=tk.NW, image=image)
window.mainloop()
filepaths = showLoadSaveDialog()
while (True):
if (filepaths[0] and len(filepaths[0]) > 1):
makeOverlayWindow(filepaths[0])
showLoadSaveDialog()