From 4f29110f5ed86bd7b21a07517a4227f5d22b0adf Mon Sep 17 00:00:00 2001 From: mmattmcw Date: Sat, 18 Mar 2023 19:10:11 -0400 Subject: [PATCH] Actually apply image --- fourcell/apply_image.py | 50 ++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/fourcell/apply_image.py b/fourcell/apply_image.py index 4818094..cbbf3fd 100644 --- a/fourcell/apply_image.py +++ b/fourcell/apply_image.py @@ -9,19 +9,45 @@ 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) + rgb_color=(255,255,255) + color = tuple(reversed(rgb_color)) + + (w, h), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_PLAIN, fontSize, 2) + image = np.zeros((h + baseline, w, 3), np.uint8) + image[:] = color + 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) : if i == 0 or i == 1 : - return 90 - if i == 2 or i == 3 : return -90 + if i == 2 or i == 3 : + return 90 def to_text (fileName) : parts = fileName.split('.') @@ -115,7 +141,8 @@ 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) +dpi = round(tmpl['width'] / 8.5) +fontSize = round((dpi / 48) / 2) #blank = cv2.imread(templateFile.replace('.calibration.json', '')) 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}'])) rotation = to_rotation(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])}') output = cv2.cvtColor(output, cv2.COLOR_BGRA2BGR)