Added .gitignore, fixed applying filters, added dpi processing.

master
Rostislav Lán 2 years ago
parent 907bfa3508
commit 605dbe0c98

2
.gitignore vendored

@ -0,0 +1,2 @@
__pycache__/
/res

@ -14,6 +14,14 @@ class filter:
self.output_file = output_file self.output_file = output_file
self.img = img 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): class filter_average(filter):
def __init__(self, input_file, output_file, img): def __init__(self, input_file, output_file, img):
@ -21,9 +29,8 @@ class filter_average(filter):
def apply(self): def apply(self):
kernel = np.ones((5, 5), np.float32)/25 kernel = np.ones((5, 5), np.float32)/25
dst = cv.filter2D(self.img, -1, kernel) self.img = cv.filter2D(self.img, -1, kernel)
plt.xticks([]), plt.yticks([]) plt.imshow(self.img)
plt.imshow(dst)
print("Average filter") print("Average filter")
@ -32,9 +39,8 @@ class filter_blur(filter):
super().__init__(input_file, output_file, img) super().__init__(input_file, output_file, img)
def apply(self): def apply(self):
blur = cv.blur(self.img, (5, 5)) self.img = cv.blur(self.img, (5, 5))
plt.xticks([]), plt.yticks([]) plt.imshow(self.img)
plt.imshow(blur)
print("Blurring filter") print("Blurring filter")
@ -43,6 +49,6 @@ class filter_gaussian(filter):
super().__init__(input_file, output_file, img) super().__init__(input_file, output_file, img)
def apply(self): def apply(self):
blur = cv.GaussianBlur(self.img, (5, 5), 0) self.img = cv.GaussianBlur(self.img, (5, 5), 0)
plt.xticks([]), plt.yticks([]) plt.imshow(self.img)
print("Gaussian filter") print("Gaussian filter")

@ -12,25 +12,36 @@ import matplotlib.pyplot as plt
# Import custom image filter library # Import custom image filter library
import filters as flt import filters as flt
class app: class apply_filters:
def __init__(self): def __init__(self):
self.parse_arguments() self.parse_arguments()
self.input_file = self.args.input_file self.input_file = self.args.input_file
self.output_file = self.args.output_file self.output_file = self.args.output_file
self.img = plt.imread(self.input_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.filters = self.args.filters
self.apply_filters() self.apply_filter()
def parse_arguments(self): def parse_arguments(self):
parser = ap.ArgumentParser(prog = 'main.py', description = 'loads and stores image') 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("-i", "--input_file", help = "Input file", required = True)
parser.add_argument("-o", "--output_file", help = "Output 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('-d', "--dpi", type = int, required = True)
#parser.add_argument( '-s', '--size') # file with default presets
#parser.add_argument('-pf', "--preset_file")
parser.add_argument('filters', type = str, nargs = '*') parser.add_argument('filters', type = str, nargs = '*')
self.args = parser.parse_args() 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): def filter_factory(self, filter_name):
if filter_name == "average": if filter_name == "average":
return flt.filter_average return flt.filter_average
@ -41,14 +52,32 @@ class app:
else: else:
raise ValueError("Invalid filter name") raise ValueError("Invalid filter name")
def apply_filters(self): def resize_img(self):
for filter in self.filters: # resize img to the new width
filter = self.filter_factory(filter) 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) filter.apply(self)
self.save_image() 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): def save_image(self):
# Save processed image # Save processed image
plt.xticks([]), plt.yticks([])
plt.axis('off')
plt.savefig(self.output_file) plt.savefig(self.output_file)
app = app() app = apply_filters()

Loading…
Cancel
Save