From afd153ef130374c079dd94b766a7367485357f9d Mon Sep 17 00:00:00 2001 From: mmattmcw Date: Tue, 14 Mar 2023 16:43:38 -0400 Subject: [PATCH] In the process of working out how to generate text to size --- fourcell/apply_image.py | 28 +++++++++++++++++++++++++++- fourcell/notes/generate_text.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 fourcell/notes/generate_text.py diff --git a/fourcell/apply_image.py b/fourcell/apply_image.py index c9b80d1..4818094 100644 --- a/fourcell/apply_image.py +++ b/fourcell/apply_image.py @@ -6,6 +6,28 @@ from os.path import exists, basename from common import image_resize, display, read_json 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 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) blank = create_blank(tmpl['width'], tmpl['height']) +fontSize = round(tmpl['width'] * fontConstant) #blank = cv2.imread(templateFile.replace('.calibration.json', '')) output = blank.copy() @@ -104,8 +127,11 @@ for i in hp: 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) + #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])) + generate_text(text, fontSize, rotation) print(f'Applied {basename(images[i])}') output = cv2.cvtColor(output, cv2.COLOR_BGRA2BGR) diff --git a/fourcell/notes/generate_text.py b/fourcell/notes/generate_text.py new file mode 100644 index 0000000..7a6c438 --- /dev/null +++ b/fourcell/notes/generate_text.py @@ -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) \ No newline at end of file