Generate a calibration sheet

This commit is contained in:
Matt McWilliams 2023-02-21 22:18:14 -05:00
parent 01c67e16dc
commit a51a4b6a23
2 changed files with 126 additions and 1 deletions

109
fourcell/calibrate.py Normal file
View File

@ -0,0 +1,109 @@
import argparse
import sys
import cv2
import numpy as np
from os.path import exists, basename
from json import dumps
from common import display, draw_text
IN=25.4
DPI=600
W=215.9
H=279.4
ACTIONS=['generate', 'calibrate', 'scale']
MEASUREMENTS={
'A' : { 'x' : 10.0 }, # X 10mm
'B' : { 'x' : 50.0 }, # X 50mm
'C' : { 'x' : 100.0 }, # X 100mm
'D' : { 'y' : 10.0 }, # Y 10mm
'E' : { 'y' : 50.0 }, # Y 50mm
'F' : { 'y' : 100.0 } # Y 100mm
}
def create_blank(width, height):
rgb_color=(255,255,255)
image = np.zeros((height, width, 3), np.uint8)
color = tuple(reversed(rgb_color))
image[:] = color
return image
def generate (args) :
print('generate')
print('--------')
dpi = DPI
if args.dpi is not None :
dpi = args.dpi
dpmm = dpi / IN
print(f'DPI:{dpi}')
print(f'DPMM:{dpmm}')
w = int(W * dpmm)
h = int(H * dpmm)
blank = create_blank(w, h)
print(f'Created {w},{h}')
topLeftX = int(round(w * 0.2))
topLeftY = int(round(h * 0.2))
bottomRightX = int(round(w * 0.8))
bottomRightY = int(round(h * 0.8))
hundred = int(round(100.0 * dpmm))
five = int(round(2.5 * dpmm))
cv2.line(blank, (topLeftX, topLeftY,), (topLeftX + hundred, topLeftY,), [0, 0, 0], 1)
cv2.line(blank, (topLeftX, topLeftY,), (topLeftX, topLeftY + hundred,), [0, 0, 0], 1)
for i in MEASUREMENTS.keys() :
print(i)
if 'x' in MEASUREMENTS[i] is not None:
print(MEASUREMENTS[i]['x'])
x = int(round(MEASUREMENTS[i]['x'] * dpmm))
cv2.line(blank, (topLeftX + x, topLeftY + five,), (topLeftX + x, topLeftY - five,), [0, 0, 0], 1)
draw_text(blank, (topLeftX + x, topLeftY - five - int(2 * dpmm),), f'{i}', dpi)
elif 'y' in MEASUREMENTS[i] is not None:
print(MEASUREMENTS[i]['y'])
y = int(round(MEASUREMENTS[i]['y'] * dpmm))
cv2.line(blank, (topLeftX + five, topLeftY + y,), (topLeftX - five, topLeftY + y,), [0, 0, 0], 1)
draw_text(blank, (topLeftX + five + int(2 * dpmm), topLeftY + y,), f'{i}', dpi)
#display(blank)
cv2.imwrite('test.jpg', blank)
def calibrate (args) :
print('calibrate')
print('---------')
json = {}
name = input('Printer name: ')
json['name'] = name
for m in MEASUREMENTS.keys():
val = input(f'{m}: ')
json[m] = float(val)
txt=dumps(json, sort_keys = True, indent = 4)
if args.output is None :
print(f'{txt}')
else :
print(f'Wrote file to {args.output}')
def scale (args) :
print('scale')
print('-----')
def main () :
parser = argparse.ArgumentParser()
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('--input', '-i', help='Input file', required=False)
parser.add_argument('--output', '-o', help='Output file', required=False)
args = parser.parse_args()
if args.action == 'generate' :
generate(args)
elif args.action == 'calibrate' :
calibrate(args)
elif args.action == 'scale' :
scale(args)
main()

View File

@ -126,4 +126,20 @@ def read_json (textPath) :
jsonOut = {}
with open(textPath) as json:
jsonOut = load(json)
return jsonOut
return jsonOut
def draw_text (original, bl, text, dpi) :
font = cv2.FONT_HERSHEY_SIMPLEX
bottomLeftCornerOfText = bl
fontScale = dpi / 96
fontColor = (0, 0, 0)
thickness = 2
lineType = 2
cv2.putText(original, text,
bottomLeftCornerOfText,
font,
fontScale,
fontColor,
thickness,
lineType)