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}..." echo "Using frames in ${OUTPUT}..."
NAME=$(basename "${OUTPUT}")
DIGITS=6 DIGITS=6
SCALE=$(echo "scale=$DIGITS; 323/404" | bc) SCALE=$(echo "scale=$DIGITS; 323/404" | bc)
OUTER=$(echo "scale=$DIGITS; 374/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}" bash fourcell/white_fourcell.sh $DPI $RATIO "${f4}" "${dest}"
args="${args} ${dest}" args="${args} ${dest}"
fi fi
bash apply_image.sh ${3}/page_$i.tiff "${1}" ${args} bash apply_image.sh ${NAME} ${3}/${NAME}_$i.tiff "${1}" ${args}
done done
echo "Wrote ${3}/page_$i.tiff" echo "Wrote ${3}/${NAME}_$i.tiff"
# x determine dpi # x determine dpi
# convert frame to white_fourcell # convert frame to white_fourcell

View File

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