Actually apply image

This commit is contained in:
Matt McWilliams 2023-03-18 19:10:11 -04:00
parent 033b400294
commit 4f29110f5e
1 changed files with 39 additions and 11 deletions

View File

@ -9,19 +9,45 @@ holeConstant = .0156862745 # 160/10200
fontConstant = 1/6000 fontConstant = 1/6000
def generate_text (text, fontSize, rotation) : def generate_text (text, fontSize, rotation) :
print(rotation) rgb_color=(255,255,255)
print(fontSize) color = tuple(reversed(rgb_color))
print(text)
print(len(text)) (w, h), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_PLAIN, fontSize, 2)
w = (fontSize * len(text)) image = np.zeros((h + baseline, w, 3), np.uint8)
h = (fontSize) image[:] = color
image = cv2.putText(image, text, (w/2, h/2), cv2.FONT_HERSHEY_PLAIN, 1, [0, 0, 0], 3, cv2.LINE_AA) image = cv2.cvtColor(image, cv2.COLOR_BGR2BGRA)
image = cv2.putText(image, text, (0, h + round(baseline/2)), cv2.FONT_HERSHEY_PLAIN, fontSize, [0, 0, 0], 2, cv2.LINE_AA)
if rotation == 90 :
image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
elif rotation == -90 :
image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
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}')
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
elif i == 2 or i == 3 :
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
def to_rotation (i) : def to_rotation (i) :
if i == 0 or i == 1 : if i == 0 or i == 1 :
return 90
if i == 2 or i == 3 :
return -90 return -90
if i == 2 or i == 3 :
return 90
def to_text (fileName) : def to_text (fileName) :
parts = fileName.split('.') parts = fileName.split('.')
@ -115,7 +141,8 @@ 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'])
fontSize = round(tmpl['width'] * fontConstant) dpi = round(tmpl['width'] / 8.5)
fontSize = round((dpi / 48) / 2)
#blank = cv2.imread(templateFile.replace('.calibration.json', '')) #blank = cv2.imread(templateFile.replace('.calibration.json', ''))
output = blank.copy() output = blank.copy()
@ -131,7 +158,8 @@ for i in range(0, len(images)) :
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(images[i]))
generate_text(text, fontSize, rotation) text_image = generate_text(text, fontSize, rotation)
output = place_text(output, text_image, i, tmpl[f'{i}'])
print(f'Applied {basename(images[i])}') print(f'Applied {basename(images[i])}')
output = cv2.cvtColor(output, cv2.COLOR_BGRA2BGR) output = cv2.cvtColor(output, cv2.COLOR_BGRA2BGR)