|
|
|
@ -7,17 +7,18 @@
|
|
|
|
|
import argparse as ap
|
|
|
|
|
import sys
|
|
|
|
|
import json
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
#from datetime import datetime
|
|
|
|
|
|
|
|
|
|
# Libraries for image processing
|
|
|
|
|
import numpy as np
|
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
from PIL import Image
|
|
|
|
|
#from PIL import Image
|
|
|
|
|
import cv2 as cv
|
|
|
|
|
|
|
|
|
|
# Import custom image filter library
|
|
|
|
|
import filters as flt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class apply_filters:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
# Parse arguments from command line
|
|
|
|
@ -34,37 +35,38 @@ class apply_filters:
|
|
|
|
|
self.preset_name = self.args.config[1]
|
|
|
|
|
self.config = json.load(open(self.config_file))
|
|
|
|
|
self.parse_conf()
|
|
|
|
|
# If no preset name given, create one from time
|
|
|
|
|
#self.preset_name = "preset_" + datetime.now().strftime("%d_%m_%Y_%H_%M_%S")
|
|
|
|
|
|
|
|
|
|
# If no config file given, expect filters in command line
|
|
|
|
|
else:
|
|
|
|
|
self.filters = self.args.filters
|
|
|
|
|
|
|
|
|
|
# Convert dimensions
|
|
|
|
|
self.img = Image.open(self.input_file)
|
|
|
|
|
if self.img is None:
|
|
|
|
|
sys.exit("Could not load the fingerprint.")
|
|
|
|
|
#self.convert_dpi()
|
|
|
|
|
#self.resize_image()
|
|
|
|
|
#read as numpy.array
|
|
|
|
|
self.img = plt.imread(self.input_file)
|
|
|
|
|
|
|
|
|
|
self.width = self.img.shape[1]
|
|
|
|
|
self.height = self.img.shape[0]
|
|
|
|
|
print(self.width, self.height)
|
|
|
|
|
|
|
|
|
|
fig = plt.figure(figsize = (self.width/self.dpi, self.height/self.dpi),
|
|
|
|
|
frameon = False, dpi = self.dpi)
|
|
|
|
|
|
|
|
|
|
#convert to numpy array for further processing
|
|
|
|
|
self.img = np.array(self.img)
|
|
|
|
|
ax = plt.Axes(fig, [0., 0., 1., 1.])
|
|
|
|
|
ax.set_axis_off()
|
|
|
|
|
fig.add_axes(ax)
|
|
|
|
|
|
|
|
|
|
if self.mirror:
|
|
|
|
|
self.mirror_image()
|
|
|
|
|
|
|
|
|
|
# Apply all filters
|
|
|
|
|
self.apply_filter()
|
|
|
|
|
self.apply_filter(fig, ax)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_conf(self):
|
|
|
|
|
|
|
|
|
|
# Parse configuration file if given.
|
|
|
|
|
try:
|
|
|
|
|
self.filters = self.config[self.preset_name]
|
|
|
|
|
except(KeyError):
|
|
|
|
|
print("Preset not found", file=sys.stderr)
|
|
|
|
|
print("Preset not found", file = sys.stderr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_arguments(self):
|
|
|
|
@ -96,24 +98,17 @@ class apply_filters:
|
|
|
|
|
def filter_factory(self, filter_name):
|
|
|
|
|
# selects filter method of filters library
|
|
|
|
|
# better this than a 100 if/else
|
|
|
|
|
print("Applying " + filter_name + " filter", file = sys.stderr)
|
|
|
|
|
return getattr(flt, filter_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def convert_dpi(self):
|
|
|
|
|
|
|
|
|
|
# conversion from inches to milimeters
|
|
|
|
|
self.size = np.empty(2)
|
|
|
|
|
self.size[0] = self.img.size[0] # / self.dpi * 25.4 # width
|
|
|
|
|
self.size[1] = self.img.size[1] # / self.dpi * 25.4 # height
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def resize_image(self):
|
|
|
|
|
|
|
|
|
|
# open image as python image object
|
|
|
|
|
print("Resize image", file = sys.stderr)
|
|
|
|
|
|
|
|
|
|
self.convert_dpi()
|
|
|
|
|
#self.img = self.img.resize((np.array(self.size)).astype(int))
|
|
|
|
|
self.img = self.img.resize((np.array(self.width, self.height)).astype(int))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def mirror_image(self):
|
|
|
|
@ -124,17 +119,16 @@ class apply_filters:
|
|
|
|
|
self.img = cv.flip(self.img, 1) # 1 for vertical mirror
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_filter(self):
|
|
|
|
|
def apply_filter(self, fig, ax):
|
|
|
|
|
|
|
|
|
|
if len(self.filters) == 0:
|
|
|
|
|
# save original image
|
|
|
|
|
filter = flt.filter_none
|
|
|
|
|
filter.apply(self)
|
|
|
|
|
# No filter given, just save the image
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
for filter_name in self.filters:
|
|
|
|
|
filter = self.filter_factory(filter_name)
|
|
|
|
|
filter.apply(self)
|
|
|
|
|
self.save_image()
|
|
|
|
|
self.save_image(fig, ax)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def print_size(self, size):
|
|
|
|
@ -142,14 +136,11 @@ class apply_filters:
|
|
|
|
|
print("Height: " + str(size[1]), file = sys.stderr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def save_image(self):
|
|
|
|
|
def save_image(self, fig, ax):
|
|
|
|
|
# Save processed image.
|
|
|
|
|
plt.xticks([]), plt.yticks([])
|
|
|
|
|
plt.axis('off')
|
|
|
|
|
print("Saving image", file = sys.stderr)
|
|
|
|
|
|
|
|
|
|
# TODO idk what dpi means, and if it should be put in here
|
|
|
|
|
plt.savefig(self.output_file)#, dpi=self.dpi)
|
|
|
|
|
ax.imshow(self.img)
|
|
|
|
|
fig.savefig(fname=self.output_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app = apply_filters()
|
|
|
|
|