Reworked config structure, loading config

master
Rostislav Lán 2 years ago
parent 9b90ee67c9
commit a19da21734

@ -1,37 +1,16 @@
{ {
"default": { "default": [
"inputFile": "res/img.png", "gaussian",
"outputFile": "res/img_done.png", "erode",
"dpi": 500, "blur"
"flip": false, ],
"width": 0, "test1": [
"filters": [ "gaussian",
"gaussian", "erode",
"erode", "blur"
"blur" ],
] "test2": [
}, "gaussian",
"test1": { "dilate"
"inputFile": "res/test_fp.png", ]
"outputFile": "res/test_fp_cpy.png",
"dpi": 500,
"flip": true,
"width": 0,
"filters": [
"gaussian",
"erode",
"blur"
]
},
"test2": {
"inputFile": "res/test_fp.png",
"outputFile": "res/test_fp_cpy.png",
"dpi": 1000,
"flip": false,
"width": 500,
"filters": [
"gaussian",
"dilate"
]
}
} }

@ -20,46 +20,39 @@ import filters as flt
class apply_filters: class apply_filters:
def __init__(self): def __init__(self):
# Parse arguments from command line
if len(sys.argv) < 4: self.parse_arguments()
# Parse configuration from json file self.input_file = self.args.input_file
self.conf_fn = sys.argv[1] self.output_file = self.args.output_file
self.preset_name = sys.argv[2] self.dpi = self.args.dpi
try: self.filters = self.args.filters
self.conf_file = open(self.conf_fn) self.mirror = self.args.mirror if self.args.mirror else 0
except(FileNotFoundError):
print("Creating new file") # Parse configuration from json file
self.conf_file = open(self.conf_fn, "x") if self.args.config:
self.conf = json.load(self.conf_file) self.config_file = self.args.config[0]
self.preset_name = self.args.config[1]
self.config = json.load(open(self.config_file))
self.parse_conf() 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: else:
# Parse arguments from command line
self.parse_arguments()
self.input_file = self.args.input_file
self.output_file = self.args.output_file
self.dpi = self.args.dpi
self.filters = self.args.filters self.filters = self.args.filters
self.flip = self.args.flip
self.width = self.args.width if self.args.width else 0
# Save preset to config file
if self.args.config_file:
self.conf_file = self.args.config_file
if self.args.preset:
self.preset_name = self.args.preset
else:
# If no preset name given, create one from time
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
parser.add_argument('-c', "--config_file", help = "Config file to save the preset into")
# new preset name # file with configuration containing presets, new preset name
parser.add_argument('-p', "--preset", help = "Name of the newly created preset") # pair argument - give both or none
parser.add_argument('--config', nargs=2, metavar=('config_file', 'preset'),
help='Config file with presets, name of the 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 self.convert_dpi()
if self.width: #self.img = self.img.resize((np.array(self.size)).astype(int))
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.img = self.img.resize((np.array(self.size)).astype(int))
#convert to numpy array for further processing
self.img = np.array(self.img)
def flip_image(self): def mirror_image(self):
# flip image when mirroring is needed # mirror 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):

Loading…
Cancel
Save