|
|
|
@ -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()
|
|
|
|
|