207 lines
7.6 KiB
Python
207 lines
7.6 KiB
Python
import tkinter as t
|
|
from PIL import ImageTk, Image
|
|
from desktopmagic.screengrab_win32 import getDisplayRects, saveScreenToBmp, saveRectToBmp, getScreenAsImage, getRectAsImage, getDisplaysAsImages
|
|
import time
|
|
|
|
RESIZE_FACTOR = 0.8
|
|
DIFFERENCE_THRESHOLD = 1 # in %
|
|
|
|
class Application:
|
|
|
|
def __init__(self):
|
|
self.main = t.Tk()
|
|
|
|
self.vcmp_float = (self.main.register(validate_float))
|
|
self.vcmp_int = (self.main.register(validate_int))
|
|
self.selected_screen = t.IntVar()
|
|
self.selected_screen.set(1)
|
|
self.selected_rect = [(0,0), (0,0)]
|
|
self.started_selection = False
|
|
self.selection_obj = None
|
|
self.previous = None
|
|
self.current = None
|
|
self.interval = 2
|
|
self.threshold = DIFFERENCE_THRESHOLD
|
|
self.current_preview = None
|
|
self.current_diff = None
|
|
self.img_counter = 0
|
|
self.screen_selection()
|
|
|
|
self.main.mainloop()
|
|
|
|
def screen_selection(self):
|
|
self.clear()
|
|
|
|
for display_num, image in enumerate(getDisplaysAsImages(), 1):
|
|
frame = t.Frame(self.main)
|
|
label = t.Radiobutton(frame, text=f"Bildschirm {display_num}", variable=self.selected_screen, value=display_num)
|
|
label.pack(side=t.TOP)
|
|
image = image.resize((320, 180), Image.ANTIALIAS)
|
|
preview = ImageTk.PhotoImage(image)
|
|
panel = t.Label(frame, image=preview)
|
|
panel.image = preview
|
|
panel.pack(side=t.TOP)
|
|
#panel.bind("<Button-1>", lambda e:self.selected_screen.set(display_num))
|
|
frame.pack(side=t.LEFT, padx=20, pady=20)
|
|
|
|
config_frame = t.Frame(self.main)
|
|
|
|
interval_label = t.Label(config_frame, text="Intervall (in Sekunden):")
|
|
interval_label.pack(side=t.TOP, pady=(10,0))
|
|
self.interval_entry = t.Entry(config_frame, validate="key", validatecommand=(self.vcmp_int, "%P"))
|
|
self.interval_entry.insert(0, str(self.interval))
|
|
self.interval_entry.pack(side=t.TOP, pady=(0, 30))
|
|
|
|
threshold_label = t.Label(config_frame, text="Schwelle (in Prozent):")
|
|
threshold_label.pack(side=t.TOP, pady=(10,0))
|
|
self.threshold_entry = t.Entry(config_frame, validate="key", validatecommand=(self.vcmp_float, "%P"))
|
|
self.threshold_entry.insert(0, str(self.threshold))
|
|
self.threshold_entry.pack(side=t.TOP, pady=(0,30))
|
|
|
|
ok_button = t.Button(config_frame, text="Bereich auswählen", command=self.rect_selection)
|
|
ok_button.pack(side=t.TOP, pady=30)
|
|
|
|
config_frame.pack(side=t.LEFT, padx=20)
|
|
|
|
|
|
|
|
def rect_selection(self):
|
|
self.interval = int(self.interval_entry.get())
|
|
self.threshold = float(self.threshold_entry.get())
|
|
self.clear()
|
|
|
|
screen_no = t.Label(self.main, text=f"Nutze Bildschirm {self.selected_screen.get()}")
|
|
screen_no.pack(side=t.TOP)
|
|
change_screen = t.Button(self.main, text="Bildschirm wechseln",
|
|
command=self.screen_selection)
|
|
change_screen.pack()
|
|
|
|
instruction = t.Label(self.main, text="Wähle einen Bereich auf dem Bildschirm aus:")
|
|
instruction.pack(side=t.TOP)
|
|
|
|
screenshot = getDisplaysAsImages()[self.selected_screen.get() - 1]
|
|
dim = tuple([int(v * RESIZE_FACTOR) for v in screenshot.size])
|
|
screenshot = screenshot.resize(dim, Image.ANTIALIAS)
|
|
screenshot = ImageTk.PhotoImage(screenshot)
|
|
self._screenshot = screenshot
|
|
screen = t.Canvas(self.main, width=dim[0], height=dim[1])
|
|
screen.create_image(0, 0, image=screenshot, anchor=t.NW)
|
|
screen.pack(side=t.TOP)
|
|
|
|
ok_button = t.Button(self.main, text="Los gehts!", command=self.capture_screen)
|
|
ok_button.pack(side=t.TOP)
|
|
|
|
screen.bind("<Button-1>", lambda e: self.start_selection(e, screen))
|
|
screen.bind("<ButtonRelease-1>", lambda e: self.end_selection(e, screen))
|
|
screen.bind("<Motion>", lambda e: self.selection_motion(e, screen))
|
|
|
|
def capture_screen(self):
|
|
self.clear()
|
|
|
|
self.next_screenshot = self.interval
|
|
self.timer_label = t.Label(self.main, text="Starte Aufnahme...")
|
|
self.timer_label.pack(side=t.TOP)
|
|
|
|
self.current_preview = t.Label(self.main)
|
|
self.current_preview.pack(side=t.TOP)
|
|
|
|
self.current_diff = t.Label(self.main, text="Bisher wurde noch kein Bild aufgenommen.")
|
|
self.current_diff.pack(side=t.TOP)
|
|
|
|
extra_capture = t.Button(self.main, text="Trotzdem speichern", command=self.save_current)
|
|
extra_capture.pack(side=t.TOP)
|
|
|
|
self.capture()
|
|
|
|
def start_selection(self, e, screen):
|
|
self.started_selection = True
|
|
self.selected_rect[0] = (e.x / RESIZE_FACTOR, e.y / RESIZE_FACTOR)
|
|
|
|
def end_selection(self, e, screen):
|
|
self.started_selection = False
|
|
self.selected_rect[1] = (e.x / RESIZE_FACTOR, e.y / RESIZE_FACTOR)
|
|
|
|
def selection_motion(self, e, screen):
|
|
if not self.started_selection:
|
|
return
|
|
|
|
x1 = self.selected_rect[0][0] * RESIZE_FACTOR
|
|
y1 = self.selected_rect[0][1] * RESIZE_FACTOR
|
|
if self.selection_obj == None:
|
|
self.selection_obj = screen.create_rectangle(x1, y1, e.x, e.y, fill="", outline="blue", width=2)
|
|
else:
|
|
screen.coords(self.selection_obj, x1, y1, e.x, e.y)
|
|
|
|
def capture(self):
|
|
screen = getDisplaysAsImages()[self.selected_screen.get() - 1]
|
|
nw, se = self.selected_rect
|
|
screen = screen.crop([nw[0], nw[1], se[0], se[1]])
|
|
diff = self.compare(screen)
|
|
if self.current_preview != None:
|
|
self.current = screen
|
|
img = ImageTk.PhotoImage(screen)
|
|
self.current_preview.configure(image=img)
|
|
self.current_preview.image = img
|
|
if self.current_diff != None:
|
|
self.current_diff.configure(text=f"Unterschied zu vorherigem Bild: {round(diff, 2)}% - Schwelle {self.threshold}%")
|
|
if diff > self.threshold:
|
|
screen.save(f"{self.img_counter}.png")
|
|
self.img_counter += 1
|
|
self.previous = screen
|
|
self.timer()
|
|
self.main.after(self.interval*1000, self.capture)
|
|
|
|
def timer(self):
|
|
label = self.timer_label
|
|
if self.next_screenshot != 0:
|
|
label.configure(text=f"Aufnahme läuft. In {self.next_screenshot} Sekunden wird ein neues Bild aufgenommen.")
|
|
self.next_screenshot -= 1
|
|
label.after(1000, self.timer)
|
|
else:
|
|
label.configure(text="Aufnahme läuft. Bild wird aufgenommen...")
|
|
self.next_screenshot = self.interval
|
|
|
|
def compare(self, current):
|
|
if self.previous == None:
|
|
return 100
|
|
if self.previous.size != current.size:
|
|
return 100
|
|
|
|
pairs = zip(self.previous.getdata(), current.getdata())
|
|
dif = sum(abs(c1 - c2) for p1, p2 in pairs for c1, c2 in zip(p1, p2))
|
|
|
|
ncomponents = current.size[0] * current.size[1] * 3
|
|
dif_percent = (dif / 255.0 * 100) / ncomponents
|
|
|
|
return dif_percent
|
|
|
|
def save_current(self):
|
|
if self.current != None:
|
|
self.current.save(f"{self.img_counter}.png")
|
|
self.img_counter += 1
|
|
|
|
def clear(self, frame=None):
|
|
frame = self.main if frame == None else frame
|
|
for widget in frame.winfo_children():
|
|
widget.destroy()
|
|
|
|
|
|
def validate_float(P):
|
|
if P:
|
|
try:
|
|
float(P)
|
|
return True
|
|
except ValueError:
|
|
return False
|
|
|
|
def validate_int(P):
|
|
if P:
|
|
try:
|
|
int(P)
|
|
return True
|
|
except ValueError:
|
|
return False
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app = Application() |