From 605dbe0c980a55477274bddaff5595171c19af48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rostislav=20L=C3=A1n?= Date: Wed, 23 Nov 2022 11:43:17 +0100 Subject: [PATCH] Added .gitignore, fixed applying filters, added dpi processing. --- .gitignore | 2 ++ src/filters.py | 22 ++++++++++++-------- src/main.py | 55 ++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d73515b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__/ +/res diff --git a/src/filters.py b/src/filters.py index 2d39235..3e22cc3 100644 --- a/src/filters.py +++ b/src/filters.py @@ -14,6 +14,14 @@ class filter: self.output_file = output_file self.img = img +class filter_none(filter): + def __init__(self, input_file, output_file, img): + super().__init__(input_file, output_file, img) + + def apply(self): + plt.imshow(self.img) + print("No filter selected") + class filter_average(filter): def __init__(self, input_file, output_file, img): @@ -21,9 +29,8 @@ class filter_average(filter): def apply(self): kernel = np.ones((5, 5), np.float32)/25 - dst = cv.filter2D(self.img, -1, kernel) - plt.xticks([]), plt.yticks([]) - plt.imshow(dst) + self.img = cv.filter2D(self.img, -1, kernel) + plt.imshow(self.img) print("Average filter") @@ -32,9 +39,8 @@ class filter_blur(filter): super().__init__(input_file, output_file, img) def apply(self): - blur = cv.blur(self.img, (5, 5)) - plt.xticks([]), plt.yticks([]) - plt.imshow(blur) + self.img = cv.blur(self.img, (5, 5)) + plt.imshow(self.img) print("Blurring filter") @@ -43,6 +49,6 @@ class filter_gaussian(filter): super().__init__(input_file, output_file, img) def apply(self): - blur = cv.GaussianBlur(self.img, (5, 5), 0) - plt.xticks([]), plt.yticks([]) + self.img = cv.GaussianBlur(self.img, (5, 5), 0) + plt.imshow(self.img) print("Gaussian filter") diff --git a/src/main.py b/src/main.py index a75b13a..c42d3fb 100644 --- a/src/main.py +++ b/src/main.py @@ -12,25 +12,36 @@ import matplotlib.pyplot as plt # Import custom image filter library import filters as flt -class app: +class apply_filters: def __init__(self): self.parse_arguments() self.input_file = self.args.input_file self.output_file = self.args.output_file self.img = plt.imread(self.input_file) - self.dpi = self.args.dpi + self.dimensions = self.img.shape + self.print_debug(self.dimensions) + self.dpi = self.args.dpi # should be around 500-1000dpi + self.convert_dpi() self.filters = self.args.filters - self.apply_filters() + self.apply_filter() def parse_arguments(self): - parser = ap.ArgumentParser(prog='main.py', description='loads and stores image') - parser.add_argument("-i", "--input_file", help="Input file", required=True) - parser.add_argument("-o", "--output_file", help="Output file", required=True) - parser.add_argument('-d', "--dpi", type=int, required=True) - #parser.add_argument( '-s', '--size') - parser.add_argument('filters', type=str, nargs='*') + parser = ap.ArgumentParser(prog = 'main.py', description = 'loads and stores image') + parser.add_argument("-i", "--input_file", help = "Input file", required = True) + parser.add_argument("-o", "--output_file", help = "Output file", required = True) + parser.add_argument('-d', "--dpi", type = int, required = True) + # file with default presets + #parser.add_argument('-pf', "--preset_file") + parser.add_argument('filters', type = str, nargs = '*') self.args = parser.parse_args() + def convert_dpi(self): + height = self.dimensions[0] + width = self.dimensions[1] + self.height = height / self.dpi * 25.4 # conversion to milimeters + self.width = width / self.dpi * 25.4 + self.print_debug([self.height, self.width, self.dimensions[2]]) + def filter_factory(self, filter_name): if filter_name == "average": return flt.filter_average @@ -41,14 +52,32 @@ class app: else: raise ValueError("Invalid filter name") - def apply_filters(self): - for filter in self.filters: - filter = self.filter_factory(filter) + def resize_img(self): + # resize img to the new width + if(False): + print('') + + def apply_filter(self): + if len(self.filters) == 0: + # save original image + filter = flt.filter_none filter.apply(self) + else: + for filter_name in self.filters: + # only one takse effect? + filter = self.filter_factory(filter_name) + filter.apply(self) self.save_image() + def print_debug(self, dimensions): + print("Height: " + str(dimensions[0])) + print("Width: " + str(dimensions[1])) + #print("Channels: " + str(dimensions[2])) + def save_image(self): # Save processed image + plt.xticks([]), plt.yticks([]) + plt.axis('off') plt.savefig(self.output_file) -app = app() +app = apply_filters()