Completed generate text example. Can be tweaked now.

This commit is contained in:
Matt McWilliams 2023-03-16 00:03:47 -04:00
parent afd153ef13
commit 033b400294
1 changed files with 10 additions and 6 deletions

View File

@ -1,9 +1,9 @@
import cv2
import numpy as np
fontConstant = 1/6000
w=10200
fontSize = round(fontConstant*w)
dpi = round(w/8.5)
fontSize = round((dpi / 96) / 2)
#(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)
@ -17,14 +17,18 @@ def generate_text (text, fontSize, 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)
(w, h), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_PLAIN, fontSize, 2)
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)
image = cv2.putText(image, text, (0, h + round(baseline/2)), cv2.FONT_HERSHEY_PLAIN, fontSize, [0, 0, 0], 1, 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)
cv2.imwrite('test.png', image)
generate_text("example", fontSize, 90)