animation/fourcell/calibrate.py

109 lines
2.9 KiB
Python
Raw Normal View History

2023-02-22 03:18:14 +00:00
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()