Reworked config structure, loading config
This commit is contained in:
142
src/main.py
142
src/main.py
@ -20,46 +20,39 @@ import filters as flt
|
||||
|
||||
class apply_filters:
|
||||
def __init__(self):
|
||||
# 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.mirror = self.args.mirror if self.args.mirror else 0
|
||||
|
||||
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)
|
||||
|
||||
# Parse configuration from json file
|
||||
if self.args.config:
|
||||
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()
|
||||
# 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:
|
||||
# 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.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
|
||||
self.img = Image.open(self.input_file)
|
||||
self.convert_dpi()
|
||||
self.resize_image()
|
||||
self.flip_image()
|
||||
if self.img is None:
|
||||
sys.exit("Could not load the fingerprint.")
|
||||
#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
|
||||
self.apply_filter()
|
||||
@ -68,45 +61,10 @@ class apply_filters:
|
||||
def parse_conf(self):
|
||||
|
||||
# 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:
|
||||
self.width = preset['width']
|
||||
self.filters = self.config[self.preset_name]
|
||||
except(KeyError):
|
||||
self.width = 0
|
||||
|
||||
|
||||
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)
|
||||
print("Preset not found", file=sys.stderr)
|
||||
|
||||
|
||||
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("dpi", type = int, help = "Scanner dpi")
|
||||
|
||||
# predefined width
|
||||
parser.add_argument('-w', "--width", type= int, help = "Option to input predefined width")
|
||||
|
||||
# boolean switch
|
||||
parser.add_argument('-f', "--flip", help="Flip input image",
|
||||
type=bool, action=ap.BooleanOptionalAction)
|
||||
parser.add_argument('-m', "--mirror", help = "Mirror input image",
|
||||
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
|
||||
parser.add_argument('-p', "--preset", help = "Name of the newly created preset")
|
||||
# file with configuration containing presets, new preset name
|
||||
# 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
|
||||
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
|
||||
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.img = self.img.resize((np.array(self.size)).astype(int))
|
||||
|
||||
#convert to numpy array for further processing
|
||||
self.img = np.array(self.img)
|
||||
self.convert_dpi()
|
||||
#self.img = self.img.resize((np.array(self.size)).astype(int))
|
||||
|
||||
|
||||
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
|
||||
|
||||
if self.flip:
|
||||
print("Flipping image", file = sys.stderr)
|
||||
self.img = cv.flip(self.img, 1) # 1 for vertical flip
|
||||
print("Mirroring image", file=sys.stderr)
|
||||
self.img = cv.flip(self.img, 1) # 1 for vertical mirror
|
||||
|
||||
|
||||
def apply_filter(self):
|
||||
@ -209,7 +149,7 @@ class apply_filters:
|
||||
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)
|
||||
plt.savefig(self.output_file)#, dpi=self.dpi)
|
||||
|
||||
|
||||
app = apply_filters()
|
||||
|
Reference in New Issue
Block a user