Hopefully fixed dpi problems, reworked filter applying pipeline.

master
Rostislav Lán 2 years ago
parent ac7c7bf1a1
commit 1db5578cc8

Binary file not shown.

Before

Width:  |  Height:  |  Size: 197 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 146 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 363 KiB

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

Loading…
Cancel
Save