def add_text_img(img, text, pos, box=None, color=None, thickness=1, scale=1, vertical=False):
"""
Adds the given text in the image.
:param img: Input image
:param text: String text
:param pos: (x, y) in the image or relative to the given Box object
:param box: Box object. If not None, the text is placed inside the box.
:param color: Color of the text.
:param thickness: Thickness of the font.
:param scale: Font size scale.
:param vertical: If true, the text is displayed vertically. (slow)
:return:
"""
if color is None:
color = COL_WHITE
text = str(text)
top_left = pos
if box is not None:
top_left = box.move(pos).to_int().top_left()
if top_left[0] > img.shape[1]:
return
if vertical:
if box is not None:
h, w, d = box.height, box.width, 3
else:
h, w, d = img.shape
txt_img = np.zeros((w, h, d), dtype=np.uint8)
# 90 deg rotation
top_left = h - pos[1], pos[0]
cv.putText(txt_img, text, top_left, cv.FONT_HERSHEY_PLAIN, scale, color, thickness)
txt_img = ndimage.rotate(txt_img, 90)
mask = txt_img > 0
if box is not None:
im_box = img_box(img, box)
im_box[mask] = txt_img[mask]
else:
img[mask] = txt_img[mask]
else:
cv.putText(img, text, top_left, cv.FONT_HERSHEY_PLAIN, scale, color, thickness)
评论列表
文章目录