You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.8 KiB

"""! @file main.py
@brief Main file for the application
@author xlanro00
"""
# Import libraries
import argparse as ap
import json
import numpy as np
import matplotlib.pyplot as plt
# Import custom image filter library
import filters as flt
class apply_filters:
def __init__(self):
# Parse and save arguments.
self.parse_arguments()
self.input_file = self.args.input_file
self.output_file = self.args.output_file
self.dpi = self.args.dpi # should be around 500-1000dpi
self.filters = self.args.filters
# Read input image, save its dimensions
self.img = plt.imread(self.input_file)
self.dimensions = self.img.shape
self.print_debug(self.dimensions)
# Convert dimensions
self.convert_dpi()
# Apply all 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)
# 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
elif filter_name == "blur":
return flt.filter_blur
elif filter_name == "gaussian":
return flt.filter_gaussian
else:
raise ValueError("Invalid filter name")
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:
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 = apply_filters()