Created
November 20, 2020 22:24
-
-
Save EricCousineau-TRI/596f04c83da9b82d0389d3ea1d782592 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
Ah, just the (u, v)
image coordinates. In this case, from the top-left on the text bounding box, whereas org
for cv2.putText
is from the bottom-left of the text bounding box.
Very helpful. Thanks!
Thank you so much!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just curious, what does
uv
mean?