Draw registration marks and print text in a reasonable position

This commit is contained in:
Matt McWilliams 2023-03-18 21:27:09 -04:00
parent 4f29110f5e
commit 36c0cb60e0
2 changed files with 90 additions and 63 deletions

View File

@ -43,7 +43,7 @@ img_name () {
}
echo "Using frames in ${OUTPUT}..."
NAME=$(basename "${OUTPUT}")
DIGITS=6
SCALE=$(echo "scale=$DIGITS; 323/404" | bc)
OUTER=$(echo "scale=$DIGITS; 374/404" | bc)
@ -106,10 +106,10 @@ for ((i=0;i<${PAGES};++i)); do
bash fourcell/white_fourcell.sh $DPI $RATIO "${f4}" "${dest}"
args="${args} ${dest}"
fi
bash apply_image.sh ${3}/page_$i.tiff "${1}" ${args}
bash apply_image.sh ${NAME} ${3}/${NAME}_$i.tiff "${1}" ${args}
done
echo "Wrote ${3}/page_$i.tiff"
echo "Wrote ${3}/${NAME}_$i.tiff"
# x determine dpi
# convert frame to white_fourcell

View File

@ -1,3 +1,4 @@
import argparse
import sys
import cv2
import numpy as np
@ -6,7 +7,9 @@ from os.path import exists, basename
from common import image_resize, display, read_json
holeConstant = .0156862745 # 160/10200
fontConstant = 1/6000
fontConstant = 96
markConstant = 5
linesConstant = 150
def generate_text (text, fontSize, rotation) :
rgb_color=(255,255,255)
@ -26,11 +29,11 @@ def generate_text (text, fontSize, rotation) :
return image
def place_text (output, text_image, i, points) :
#print('place_text')
height, width = text_image.shape[:2]
halfW = int(round(width/2))
halfH = int(round(height/2))
#print(f'{width}x{height}')
halfW = int(round(width / 2))
halfH = int(round(height / 2))
if i == 0 or i == 1 :
y = int(round((points['0']['y'] + points['1']['y']) / 2))
x = int(round((points['0']['x'] + points['1']['x']) / 2)) - width
@ -38,7 +41,6 @@ def place_text (output, text_image, i, points) :
y = int(round((points['0']['y'] + points['1']['y']) / 2))
x = int(round((points['0']['x'] + points['1']['x']) / 2)) + width
output[y-halfH:y-halfH+height, x-halfW:x-halfW+width] = text_image
return output
@ -49,9 +51,11 @@ def to_rotation (i) :
if i == 2 or i == 3 :
return 90
def to_text (fileName) :
def to_text (fileName, project_name) :
parts = fileName.split('.')
name = parts[0]
if 'guide_' in name :
return project_name
parts = name.split('_')
return parts[len(parts)-1]
@ -99,70 +103,93 @@ def combine_images (bg, fg, mask) :
bk = cv2.bitwise_or(bg, bg, mask=mask)
return cv2.bitwise_or(fk, bk)
if len(sys.argv) < 2 :
print('Please provide an output destination file')
exit(1)
def draw_registration_marks (output, points, registrationMark, lines) :
keys = list(points.keys())
outputFile = sys.argv[1]
for key in keys:
output = draw_registration_mark(output, points[key], registrationMark, lines)
if len(sys.argv) < 3:
print('Please provide a calibration template to apply images to')
exit(2)
return output
templateFile = sys.argv[2]
def draw_registration_mark (output, point, registrationMark, lines) :
half = int(round(registrationMark / 2))
topY = point['y'] - half
bottomY = point['y'] + half
leftX = point['x'] - half
rightX = point['x'] + half
if not exists(templateFile) :
print('Calibration template does not exist, please provide one that does')
exit(3)
cv2.line(output, (leftX, point['y'],), (rightX, point['y'],), [0, 0, 0], lines)
cv2.line(output, (point['x'], topY,), (point['x'], bottomY,), [0, 0, 0], lines)
if len(sys.argv) < 4:
print('Please provide at least one image to apply')
exit(4)
return output
if len(sys.argv) > 7:
print('Please provide maximum four images to apply')
exit(5)
def apply_image (args) :
if not exists(args.template) :
print('Calibration template does not exist, please provide one that does')
exit(3)
images = []
for i in range(3, len(sys.argv)):
imagePath = sys.argv[i]
if not exists(imagePath) :
print(f'Image {imagePath} does not exist, exiting...')
exit(6)
images.append(imagePath)
if len(args.images) < 1:
print('Please provide at least one image to apply')
exit(4)
print('Using images: ')
for img in images:
print(f' -> {basename(img)}')
if len(args.images) > 4:
print('Please provide maximum four images to apply')
exit(5)
tmpl = read_json(templateFile)
for img in args.images:
if not exists(img) :
print(f'Image {img} does not exist, exiting...')
exit(6)
print(f"Image size {tmpl['width']}x{tmpl['height']}")
print('Using images: ')
for img in args.images:
print(f' -> {basename(img)}')
hole = round(tmpl['width'] * holeConstant)
blank = create_blank(tmpl['width'], tmpl['height'])
dpi = round(tmpl['width'] / 8.5)
fontSize = round((dpi / 48) / 2)
#blank = cv2.imread(templateFile.replace('.calibration.json', ''))
output = blank.copy()
tmpl = read_json(args.template)
hp = tmpl['holePunches']
for i in hp:
print(hp[i])
cv2.circle(output, (hp[i]['x'], hp[i]['y'],), hole, (200, 200, 200,), -1)
print(f"Image size {tmpl['width']}x{tmpl['height']}")
for i in range(0, len(images)) :
frame = cv2.imread(images[i])
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2RGBA)
#cv2.imwrite(f'test{i}.png', output)
output = apply_image_to_points(output, frame, to_points(tmpl[f'{i}']))
rotation = to_rotation(i)
text = to_text(basename(images[i]))
text_image = generate_text(text, fontSize, rotation)
output = place_text(output, text_image, i, tmpl[f'{i}'])
print(f'Applied {basename(images[i])}')
hole = round(tmpl['width'] * holeConstant)
blank = create_blank(tmpl['width'], tmpl['height'])
dpi = round(tmpl['width'] / 8.5)
fontSize = round(dpi / fontConstant)
registrationMark = int(round(dpi / markConstant))
lines = int(round(dpi / linesConstant))
#blank = cv2.imread(args.template.replace('.calibration.json', ''))
output = blank.copy()
output = cv2.cvtColor(output, cv2.COLOR_BGRA2BGR)
#display(output)
cv2.imwrite(outputFile, output)
##print(f'Wrote {outputFile}')
hp = tmpl['holePunches']
for i in hp:
print(hp[i])
cv2.circle(output, (hp[i]['x'], hp[i]['y'],), hole, (200, 200, 200,), -1)
for i in range(0, len(args.images)) :
print(args.images[i])
frame = cv2.imread(args.images[i])
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2RGBA)
#cv2.imwrite(f'test{i}.png', output)
output = apply_image_to_points(output, frame, to_points(tmpl[f'{i}']))
rotation = to_rotation(i)
text = to_text(basename(args.images[i]), args.name)
text_image = generate_text(text, fontSize, rotation)
output = place_text(output, text_image, i, tmpl[f'{i}'])
output = draw_registration_marks(output, tmpl[f'{i}'], registrationMark, lines)
print(f'Applied {basename(args.images[i])}')
output = cv2.cvtColor(output, cv2.COLOR_BGRA2BGR)
#display(output)
cv2.imwrite(args.destination, output)
##print(f'Wrote {args.destination}')
def main () :
parser = argparse.ArgumentParser()
parser.add_argument('name', help='Name of project')
parser.add_argument('destination', help='File to output image to')
parser.add_argument('template', help='Template file to use')
parser.add_argument('images', nargs=argparse.REMAINDER)
args = parser.parse_args()
apply_image(args)
main()