Skip to content

Instantly share code, notes, and snippets.

@EricCousineau-TRI
Created November 20, 2020 22:24
Show Gist options
  • Save EricCousineau-TRI/596f04c83da9b82d0389d3ea1d782592 to your computer and use it in GitHub Desktop.
Save EricCousineau-TRI/596f04c83da9b82d0389d3ea1d782592 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
def draw_text(
img,
*,
text,
uv_top_left,
color=(255, 255, 255),
fontScale=0.5,
thickness=1,
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
outline_color=(0, 0, 0),
line_spacing=1.5,
):
"""
Draws multiline with an outline.
"""
assert isinstance(text, str)
uv_top_left = np.array(uv_top_left, dtype=float)
assert uv_top_left.shape == (2,)
for line in text.splitlines():
(w, h), _ = cv2.getTextSize(
text=line,
fontFace=fontFace,
fontScale=fontScale,
thickness=thickness,
)
uv_bottom_left_i = uv_top_left + [0, h]
org = tuple(uv_bottom_left_i.astype(int))
if outline_color is not None:
cv2.putText(
img,
text=line,
org=org,
fontFace=fontFace,
fontScale=fontScale,
color=outline_color,
thickness=thickness * 3,
lineType=cv2.LINE_AA,
)
cv2.putText(
img,
text=line,
org=org,
fontFace=fontFace,
fontScale=fontScale,
color=color,
thickness=thickness,
lineType=cv2.LINE_AA,
)
uv_top_left += [0, h * line_spacing]
@VNVetrov
Copy link

VNVetrov commented May 3, 2023

Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment