In the process of working out how to generate text to size

This commit is contained in:
Matt McWilliams 2023-03-14 16:43:38 -04:00
parent a4b966abd0
commit afd153ef13
2 changed files with 57 additions and 1 deletions

View File

@ -6,6 +6,28 @@ 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
def generate_text (text, fontSize, rotation) :
print(rotation)
print(fontSize)
print(text)
print(len(text))
w = (fontSize * len(text))
h = (fontSize)
image = cv2.putText(image, text, (w/2, h/2), cv2.FONT_HERSHEY_PLAIN, 1, [0, 0, 0], 3, cv2.LINE_AA)
def to_rotation (i) :
if i == 0 or i == 1 :
return 90
if i == 2 or i == 3 :
return -90
def to_text (fileName) :
parts = fileName.split('.')
name = parts[0]
parts = name.split('_')
return parts[len(parts)-1]
# use top left, top right, bottom left # use top left, top right, bottom left
def apply_image_to_points (original, target, points) : def apply_image_to_points (original, target, points) :
@ -93,6 +115,7 @@ 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)
#blank = cv2.imread(templateFile.replace('.calibration.json', '')) #blank = cv2.imread(templateFile.replace('.calibration.json', ''))
output = blank.copy() output = blank.copy()
@ -104,8 +127,11 @@ for i in hp:
for i in range(0, len(images)) : for i in range(0, len(images)) :
frame = cv2.imread(images[i]) frame = cv2.imread(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)
text = to_text(basename(images[i]))
generate_text(text, fontSize, rotation)
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)

View File

@ -0,0 +1,30 @@
import cv2
import numpy as np
fontConstant = 1/6000
w=10200
fontSize = round(fontConstant*w)
#(label_width, label_height), baseline = cv2.getTextSize(label, FONT, FONT_SCALE, FONT_THICKNESS)
#label_patch = np.zeros((label_height + baseline, label_width, 3), np.uint8)
#label_patch[:,:] = bg_color
def generate_text (text, fontSize, rotation) :
rgb_color=(255,255,255)
color = tuple(reversed(rgb_color))
print(rotation)
print(fontSize)
print(text)
print(len(text))
(w, h), baseline = cv2.getTextSize(text, (round(w/2), round(h/2)), cv2.FONT_HERSHEY_PLAIN, 1, [0, 0, 0], 3, cv2.LINE_AA)
w = (fontSize * len(text))
h = (fontSize)
print(f'{w}x{h}')
image = np.zeros((h + baseline, w, 3), np.uint8)
image[:] = color
image = cv2.cvtColor(image, cv2.COLOR_BGR2BGRA)
image = cv2.putText(image, text, (round(w/2), round(h/2)), cv2.FONT_HERSHEY_PLAIN, 1, [0, 0, 0], 3, cv2.LINE_AA)
cv2.imwrite('test.png', image)
generate_text("example", fontSize, 90)