Refactor to use argparse. Add descriptions and comments for context

This commit is contained in:
Matt McWilliams 2024-01-03 13:19:04 -05:00
parent 7248c1feb1
commit c16b92397b
5 changed files with 33 additions and 25 deletions

View File

@ -183,7 +183,7 @@ def apply_image (args) :
##print(f'Wrote {args.destination}') ##print(f'Wrote {args.destination}')
def main () : def main () :
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser(description='Create a printable rasterized image that can be hole punched and animated.')
parser.add_argument('name', help='Name of project') parser.add_argument('name', help='Name of project')
parser.add_argument('destination', help='File to output image to') parser.add_argument('destination', help='File to output image to')
parser.add_argument('template', help='Template file to use') parser.add_argument('template', help='Template file to use')

View File

@ -181,7 +181,7 @@ def scale (args) :
def main () : def main () :
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser(description='Create a calibration file, apply to a scanned page, or scale image to desired DPI.')
parser.add_argument('action', help='The action to perform', choices=ACTIONS) parser.add_argument('action', help='The action to perform', choices=ACTIONS)
parser.add_argument('--dpi', '-d', help='DPI to generate', required=False, type=int) parser.add_argument('--dpi', '-d', help='DPI to generate', required=False, type=int)
parser.add_argument('--input', '-i', help='Input file', required=False) parser.add_argument('--input', '-i', help='Input file', required=False)

View File

@ -63,7 +63,7 @@ def guide (args) :
cv2.imwrite(args.output, img) cv2.imwrite(args.output, img)
def main () : def main () :
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser(description='Generate a raster image guide image for centering the camera.')
parser.add_argument('output', help='Output file') parser.add_argument('output', help='Output file')
parser.add_argument('--dpi', '-d', help='DPI', required=False, type=int, default=600) parser.add_argument('--dpi', '-d', help='DPI', required=False, type=int, default=600)
parser.add_argument('--dimensions', '-D', help='Dimensions of image', required=True, type=str) parser.add_argument('--dimensions', '-D', help='Dimensions of image', required=True, type=str)

View File

@ -1,3 +1,4 @@
import argparse
import sys import sys
import cv2 import cv2
import numpy as np import numpy as np
@ -293,22 +294,24 @@ def normalize_image(blank, rotated, offset, tl) :
# #
# NORMALIZE # NORMALIZE
# #
# The purpose of this script is to normalize scanned hole punch images to a standard
# size, with standard padding around the holes and oriented straight up and down.
# Useful for converting scanned plotter-drawn pages into 4 frame sheets and for
# generating images to print.
#
if len(sys.argv) < 2: parser = argparse.ArgumentParser(description='Normalize a scanned hole punch image.')
print('Please provide path of scan to normalize') parser.add_argument('input', type=str, help='Scan to normalize')
exit(1) parser.add_argument('output', type=str, help='Normalized file to output')
args = parser.parse_args()
if len(sys.argv) < 3: scanImage = args.input
print('Please provide path to output file') normalImage = args.output
exit(2)
scanImage = sys.argv[-2]
if not exists(scanImage) : if not exists(scanImage) :
print('Scan provided does not exist') print('Scan provided does not exist')
exit(5) exit(1)
normalImage = sys.argv[-1]
pageDim = (11, 8.5) pageDim = (11, 8.5)
pageRatio = pageDim[1] / pageDim[0] pageRatio = pageDim[1] / pageDim[0]

View File

@ -1,3 +1,4 @@
import argparse
import sys import sys
import cv2 import cv2
import numpy as np import numpy as np
@ -16,14 +17,16 @@ order = [ 0, 2, 3, 5, 4, 1 ]
matchMethods = [cv2.TM_CCOEFF, cv2.TM_CCOEFF_NORMED, cv2.TM_CCORR, cv2.TM_CCORR_NORMED, cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED] matchMethods = [cv2.TM_CCOEFF, cv2.TM_CCOEFF_NORMED, cv2.TM_CCORR, cv2.TM_CCORR_NORMED, cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]
# #
# CALIBRATE # TEMPLATE
# #
if len(sys.argv) < 2: parser = argparse.ArgumentParser(description='Create a template from normalized scan file.')
print('Please provide path of normalized scan to build template from to') parser.add_argument('normalized', type=str, help='Normalized scan to build template from.')
exit(1) parser.add_argument('registration', type=str, nargs='?', help='Registration mark to use.');
#parser.add_argument('output', type=str, help='Normalized file to output', default=None)
args = parser.parse_args()
normalImage = sys.argv[1] normalImage = args.normalized
if not exists(normalImage) : if not exists(normalImage) :
print('Normalized scan does not exist, please provide one that does') print('Normalized scan does not exist, please provide one that does')
@ -35,12 +38,12 @@ if not exists(normalText) :
print('Corresponding normalized scan text does not exist, please generate one') print('Corresponding normalized scan text does not exist, please generate one')
exit(3) exit(3)
if len(sys.argv) > 2 : if args.registraion is not None :
if not exists(sys.argv[2]) : if not exists(args.registraion) :
registrationMark = cv2.imread(sys.argv[2]) registrationMark = cv2.imread(args.registraion)
print(f'Registration mark {sys.argv[2]} does not exist') print(f'Registration mark {args.registraion} does not exist')
exit(4) exit(4)
print(f'Using registration mark {sys.argv[2]}') print(f'Using registration mark {args.registraion}')
def get_dpi (width) : def get_dpi (width) :
if width == 2550 : if width == 2550 :
@ -59,8 +62,10 @@ height, width = img.shape[:2]
orientation = height > width orientation = height > width
dpi = get_dpi(width) dpi = get_dpi(width)
try: registrationMark try:
except NameError: registrationMark = None registrationMark
except NameError:
registrationMark = None
if registrationMark is None : if registrationMark is None :
registrationMark = cv2.imread(f'./registrationMark/{dpi}.png', 0) registrationMark = cv2.imread(f'./registrationMark/{dpi}.png', 0)
w, h = registrationMark.shape[:2] w, h = registrationMark.shape[:2]