Added saving presets to JSON files.

master
Rostislav Lán 2 years ago
parent e9fc77998a
commit 151eca3f83

@ -5,9 +5,9 @@
# Import basic libraries # Import basic libraries
import argparse as ap import argparse as ap
import configparser as cp
import sys import sys
import json import json
from datetime import datetime
# Libraries for image processing # Libraries for image processing
import numpy as np import numpy as np
@ -22,24 +22,39 @@ class apply_filters:
def __init__(self): def __init__(self):
if len(sys.argv) < 4: if len(sys.argv) < 4:
# Parse configuration from file # Parse configuration from json file
print("Reading " + sys.argv[1] + self.conf_fn = sys.argv[1]
" for configuration" + sys.argv[2], file=sys.stderr) self.preset_name = sys.argv[2]
self.conf_file = open(sys.argv[1]) try:
self.preset = sys.argv[2] 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() self.parse_conf()
else: else:
# Parse and save arguments. # 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.flip = self.args.flip
self.conf_file = self.args.config_file if self.args.config_file else None
self.preset = self.args.preset if self.args.preset else None
self.width = self.args.width if self.args.width else 0 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() self.convert_dpi()
@ -53,9 +68,8 @@ 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.conf = json.load(self.conf_file)
preset = self.conf[self.preset]
self.input_file = preset['inputFile'] self.input_file = preset['inputFile']
self.output_file = preset['outputFile'] self.output_file = preset['outputFile']
self.dpi = preset['dpi'] self.dpi = preset['dpi']
@ -69,6 +83,32 @@ class apply_filters:
except(KeyError): except(KeyError):
self.width = 0 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)
def parse_arguments(self): def parse_arguments(self):
# Parse arguments # Parse arguments

Loading…
Cancel
Save