|
|
@ -20,46 +20,39 @@ import filters as flt
|
|
|
|
|
|
|
|
|
|
|
|
class apply_filters:
|
|
|
|
class apply_filters:
|
|
|
|
def __init__(self):
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
|
|
|
|
if len(sys.argv) < 4:
|
|
|
|
|
|
|
|
# Parse configuration from json file
|
|
|
|
|
|
|
|
self.conf_fn = sys.argv[1]
|
|
|
|
|
|
|
|
self.preset_name = sys.argv[2]
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
self.conf_file = open(self.conf_fn)
|
|
|
|
|
|
|
|
except(FileNotFoundError):
|
|
|
|
|
|
|
|
print("Creating new file")
|
|
|
|
|
|
|
|
self.conf_file = open(self.conf_fn, "x")
|
|
|
|
|
|
|
|
self.conf = json.load(self.conf_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.parse_conf()
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
# Parse arguments from command line
|
|
|
|
# Parse arguments from command line
|
|
|
|
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.dpi = self.args.dpi
|
|
|
|
self.dpi = self.args.dpi
|
|
|
|
self.filters = self.args.filters
|
|
|
|
self.filters = self.args.filters
|
|
|
|
self.flip = self.args.flip
|
|
|
|
self.mirror = self.args.mirror if self.args.mirror else 0
|
|
|
|
self.width = self.args.width if self.args.width else 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Save preset to config file
|
|
|
|
# Parse configuration from json file
|
|
|
|
if self.args.config_file:
|
|
|
|
if self.args.config:
|
|
|
|
|
|
|
|
self.config_file = self.args.config[0]
|
|
|
|
self.conf_file = self.args.config_file
|
|
|
|
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 self.args.preset:
|
|
|
|
# If no config file given, expect filters in command line
|
|
|
|
self.preset_name = self.args.preset
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
# If no preset name given, create one from time
|
|
|
|
self.filters = self.args.filters
|
|
|
|
self.preset_name = "preset_" + datetime.now().strftime("%d_%m_%Y_%H_%M_%S")
|
|
|
|
|
|
|
|
self.write_conf()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Convert dimensions
|
|
|
|
# Convert dimensions
|
|
|
|
self.img = Image.open(self.input_file)
|
|
|
|
self.img = Image.open(self.input_file)
|
|
|
|
self.convert_dpi()
|
|
|
|
if self.img is None:
|
|
|
|
self.resize_image()
|
|
|
|
sys.exit("Could not load the fingerprint.")
|
|
|
|
self.flip_image()
|
|
|
|
#self.convert_dpi()
|
|
|
|
|
|
|
|
#self.resize_image()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#convert to numpy array for further processing
|
|
|
|
|
|
|
|
self.img = np.array(self.img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self.mirror:
|
|
|
|
|
|
|
|
self.mirror_image()
|
|
|
|
|
|
|
|
|
|
|
|
# Apply all filters
|
|
|
|
# Apply all filters
|
|
|
|
self.apply_filter()
|
|
|
|
self.apply_filter()
|
|
|
@ -68,45 +61,10 @@ class apply_filters:
|
|
|
|
def parse_conf(self):
|
|
|
|
def parse_conf(self):
|
|
|
|
|
|
|
|
|
|
|
|
# Parse configuration file if given.
|
|
|
|
# Parse configuration file if given.
|
|
|
|
preset = self.conf[self.preset_name]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.input_file = preset['inputFile']
|
|
|
|
|
|
|
|
self.output_file = preset['outputFile']
|
|
|
|
|
|
|
|
self.dpi = preset['dpi']
|
|
|
|
|
|
|
|
self.filters = preset['filters']
|
|
|
|
|
|
|
|
self.flip = preset['flip']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Width in config file should be set to 0 even if not used,
|
|
|
|
|
|
|
|
# checking nonetheless
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
self.width = preset['width']
|
|
|
|
self.filters = self.config[self.preset_name]
|
|
|
|
except(KeyError):
|
|
|
|
except(KeyError):
|
|
|
|
self.width = 0
|
|
|
|
print("Preset not found", file=sys.stderr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def write_conf(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
temp = {}
|
|
|
|
|
|
|
|
temp_fp = open(self.conf_file, 'a+')
|
|
|
|
|
|
|
|
# If config file new or empty, loading fails
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
temp = json.load(temp_fp)
|
|
|
|
|
|
|
|
except(json.decoder.JSONDecodeError):
|
|
|
|
|
|
|
|
temp = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Create new preset
|
|
|
|
|
|
|
|
temp[self.preset_name] = ({
|
|
|
|
|
|
|
|
"inputFile": self.input_file,
|
|
|
|
|
|
|
|
"outputFile": self.output_file,
|
|
|
|
|
|
|
|
"dpi": self.dpi,
|
|
|
|
|
|
|
|
"flip": self.flip,
|
|
|
|
|
|
|
|
"width": self.width,
|
|
|
|
|
|
|
|
"filters": self.filters
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Update json file with new preset
|
|
|
|
|
|
|
|
with open(self.conf_file, 'w') as temp_fp:
|
|
|
|
|
|
|
|
json.dump(temp, temp_fp, indent=4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_arguments(self):
|
|
|
|
def parse_arguments(self):
|
|
|
@ -120,18 +78,14 @@ class apply_filters:
|
|
|
|
parser.add_argument("output_file", type = str, help = "Output file location")
|
|
|
|
parser.add_argument("output_file", type = str, help = "Output file location")
|
|
|
|
parser.add_argument("dpi", type = int, help = "Scanner dpi")
|
|
|
|
parser.add_argument("dpi", type = int, help = "Scanner dpi")
|
|
|
|
|
|
|
|
|
|
|
|
# predefined width
|
|
|
|
|
|
|
|
parser.add_argument('-w', "--width", type= int, help = "Option to input predefined width")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# boolean switch
|
|
|
|
# boolean switch
|
|
|
|
parser.add_argument('-f', "--flip", help="Flip input image",
|
|
|
|
parser.add_argument('-m', "--mirror", help = "Mirror input image",
|
|
|
|
type = bool, action = ap.BooleanOptionalAction)
|
|
|
|
type = bool, action = ap.BooleanOptionalAction)
|
|
|
|
|
|
|
|
|
|
|
|
# file with configuration containing presets
|
|
|
|
# file with configuration containing presets, new preset name
|
|
|
|
parser.add_argument('-c', "--config_file", help = "Config file to save the preset into")
|
|
|
|
# pair argument - give both or none
|
|
|
|
|
|
|
|
parser.add_argument('--config', nargs=2, metavar=('config_file', 'preset'),
|
|
|
|
# new preset name
|
|
|
|
help='Config file with presets, name of the preset')
|
|
|
|
parser.add_argument('-p', "--preset", help = "Name of the newly created preset")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# array of unknown length, all filter names saved inside
|
|
|
|
# array of unknown length, all filter names saved inside
|
|
|
|
parser.add_argument('filters', type = str, nargs = '*', help = "List of filter names")
|
|
|
|
parser.add_argument('filters', type = str, nargs = '*', help = "List of filter names")
|
|
|
@ -158,30 +112,16 @@ class apply_filters:
|
|
|
|
# open image as python image object
|
|
|
|
# open image as python image object
|
|
|
|
print("Resize image", file = sys.stderr)
|
|
|
|
print("Resize image", file = sys.stderr)
|
|
|
|
|
|
|
|
|
|
|
|
# resize if target width passed as an argument
|
|
|
|
|
|
|
|
if self.width:
|
|
|
|
|
|
|
|
self.size[0] = int(
|
|
|
|
|
|
|
|
self.width * self.img.size[0] / self.img.size[1])
|
|
|
|
|
|
|
|
self.size[1] = self.width
|
|
|
|
|
|
|
|
print(self.size)
|
|
|
|
|
|
|
|
self.img = self.img.resize(
|
|
|
|
|
|
|
|
(np.array(self.size).astype(int)))
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
self.convert_dpi()
|
|
|
|
self.convert_dpi()
|
|
|
|
#self.img = self.img.resize((np.array(self.size)).astype(int))
|
|
|
|
#self.img = self.img.resize((np.array(self.size)).astype(int))
|
|
|
|
|
|
|
|
|
|
|
|
#convert to numpy array for further processing
|
|
|
|
|
|
|
|
self.img = np.array(self.img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def mirror_image(self):
|
|
|
|
|
|
|
|
|
|
|
|
def flip_image(self):
|
|
|
|
# mirror image when mirroring is needed
|
|
|
|
|
|
|
|
|
|
|
|
# flip image when mirroring is needed
|
|
|
|
|
|
|
|
# should be used only if we want a positive form
|
|
|
|
# should be used only if we want a positive form
|
|
|
|
|
|
|
|
print("Mirroring image", file=sys.stderr)
|
|
|
|
if self.flip:
|
|
|
|
self.img = cv.flip(self.img, 1) # 1 for vertical mirror
|
|
|
|
print("Flipping image", file = sys.stderr)
|
|
|
|
|
|
|
|
self.img = cv.flip(self.img, 1) # 1 for vertical flip
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_filter(self):
|
|
|
|
def apply_filter(self):
|
|
|
|