import cv2 import numpy as np w=10200 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) #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, 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, (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)