From 749f676af222fe0d09477b2a27d7e491caded736 Mon Sep 17 00:00:00 2001 From: paumann Date: Tue, 24 May 2022 17:29:24 +0200 Subject: [PATCH] Initital commit --- .gitignore | 160 +++++++++++++++++++++++++++++++++++++++++ app.py | 207 +++++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 7 ++ 3 files changed, 374 insertions(+) create mode 100644 .gitignore create mode 100644 app.py create mode 100644 main.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6769e21 --- /dev/null +++ b/.gitignore @@ -0,0 +1,160 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..bdad190 --- /dev/null +++ b/app.py @@ -0,0 +1,207 @@ +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("", 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("", lambda e: self.start_selection(e, screen)) + screen.bind("", lambda e: self.end_selection(e, screen)) + screen.bind("", 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() \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..7fe462e --- /dev/null +++ b/main.py @@ -0,0 +1,7 @@ +from app import Application + + +from app import Application + +if __name__ == "__main__": + app = Application() \ No newline at end of file