From 5483818309fa960356834ec5a9ef61a26600003c Mon Sep 17 00:00:00 2001 From: Adam Jurkiewicz Pythonista Local Date: Fri, 19 Dec 2025 15:02:26 +0100 Subject: [PATCH] cv --- dzien_05/gui/funkcje/__init__.py | 0 dzien_05/gui/funkcje/obraz.py | 94 ++++++++++++++++++++++++++++++++ dzien_05/gui/gui02.py | 14 ++++- 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 dzien_05/gui/funkcje/__init__.py create mode 100644 dzien_05/gui/funkcje/obraz.py diff --git a/dzien_05/gui/funkcje/__init__.py b/dzien_05/gui/funkcje/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dzien_05/gui/funkcje/obraz.py b/dzien_05/gui/funkcje/obraz.py new file mode 100644 index 0000000..da1ceb6 --- /dev/null +++ b/dzien_05/gui/funkcje/obraz.py @@ -0,0 +1,94 @@ +import os + +import cv2 +import cv2 as cv +import os + +class Obraz: + def __init__(self, filename:str, mode:int = cv.IMREAD_COLOR_BGR): + self.filename = filename + self.mode = mode + self.loaded = False + self.img = None + self.img_other = None + self.transform_name = "NOTHING" + + def load_image(self): + if self.loaded: + return True + if not os.path.exists(self.filename): + return False + # raise FileNotFoundError(self.filename) + try: + self.img = cv.imread(self.filename, flags=self.mode) + self.loaded = True + except Exception as e: + print(e) + + return True + + def show_image(self, window_name:str="Window"): + if not self.loaded: + return False + window_name += f" - {self.filename}" + cv.imshow(window_name, self.img) + cv.waitKey(0) + cv.destroyAllWindows() + return None + + def show_image_other(self, window_name:str="Window after "): + if not self.loaded: + return False + if self.img_other is None: + return False + window_name += f" - {self.filename} - {self.transform_name}" + combined = cv.hconcat([self.img, self.img_other]) + cv.imshow(window_name, combined) + cv.waitKey(0) + cv.destroyAllWindows() + return None + + def transform_equalize_histogram(self): + if not self.loaded: + return False + if self.mode == cv.IMREAD_COLOR or self.mode == cv.IMREAD_COLOR_BGR: + gray_image = cv2.cvtColor(self.img, cv2.COLOR_BGR2GRAY) + self.img_other = cv.equalizeHist(gray_image) + self.transform_name = "Equalize Histogram" + return True + + def transform_smoothing(self, smoothing_type="GaussianBlur"): + """ + + :param smoothing_type: GaussianBlur (default) or medianBlur + :return: True or False + """ + if not self.loaded: + return False + try: + if smoothing_type == "GaussianBlur": + self.img_other = cv.GaussianBlur(self.img, (5, 5), 0) + else: + self.img_other = cv.medianBlur(self.img, 5) + self.transform_name = smoothing_type + return True + except Exception as e: + print(e) + return False + + def return_image_stream(self, type:str="other"): + """ + + :param type: other | original + :return: + """ + if not self.loaded: + return None + if type == "original": + return self.img + elif type == "other": + if self.img_other is None: + return None + return self.img_other + else: + return None diff --git a/dzien_05/gui/gui02.py b/dzien_05/gui/gui02.py index fc5d29c..8ff19ae 100644 --- a/dzien_05/gui/gui02.py +++ b/dzien_05/gui/gui02.py @@ -1,3 +1,4 @@ +from funkcje.obraz import Obraz import FreeSimpleGUI as sg # sg.theme('DarkAmber') # Add a touch of color @@ -7,7 +8,7 @@ layout = [ [sg.Text('Wybrana data'), sg.In(key='data') ,sg.CalendarButton("Data [sg.Text('Enter something on Row 2'), sg.InputText(key="row2")], [sg.Text('Enter something on Row XX'), sg.InputText(key="inna")], [sg.Text('Wybrany plik'), sg.In(key='input'), sg.FileBrowse(target='input')], - [sg.Button('Ok'), sg.Button('Przerwij'), sg.Button('HELP'), sg.Button("AKCJA")] ] + [sg.Button('Ok'), sg.Button('Przerwij'),sg.Button("OBRAZ"), sg.Button('HELP'), sg.Button("AKCJA")] ] # ZADANIE: # dodaj przycisk o nazwie SYSTEM, wtedy na ekranie ma być # print(sys.version) @@ -33,6 +34,17 @@ while True: dane = plik_odczyt.read() sg.easy_print(dane) + if event =="OBRAZ": + pliczek = values['input'] + if pliczek: + if pliczek[-3:] == 'png': + obrazek = Obraz(pliczek) + if obrazek.load_image(): + obrazek.show_image() + obrazek.transform_smoothing() + obrazek.show_image_other() + + if event == "Ok": sg.easy_print(f"{values=}")