Skip to content

Commit

Permalink
Add font_path to draw
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Apr 26, 2020
1 parent cec811e commit efcea27
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
21 changes: 12 additions & 9 deletions imgviz/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,19 @@ def rectangle(src, aabb1, aabb2, fill=None, outline=None, width=0):
return np.array(dst)


def _get_font(size):
def _get_font(size, font_path=None):
import matplotlib

fonts_path = osp.join(
osp.dirname(matplotlib.__file__), "mpl-data/fonts/ttf"
)
font_path = osp.join(fonts_path, "DejaVuSansMono.ttf")
if font_path is None:
fonts_path = osp.join(
osp.dirname(matplotlib.__file__), "mpl-data/fonts/ttf"
)
font_path = osp.join(fonts_path, "DejaVuSansMono.ttf")
font = PIL.ImageFont.truetype(font=font_path, size=size)
return font


def text_size(text, size):
def text_size(text, size, font_path=None):
"""Get text size (height and width).
Parameters
Expand All @@ -220,15 +221,15 @@ def text_size(text, size):
Text width.
"""
font = _get_font(size)
font = _get_font(size, font_path=font_path)
lines = text.splitlines()
n_lines = len(lines)
longest_line = max(lines, key=len)
width, height = font.getsize(longest_line)
return height * n_lines, width


def text(src, yx, text, size, color=(0, 0, 0)):
def text(src, yx, text, size, color=(0, 0, 0), font_path=None):
"""Draw text on numpy array with Pillow.
Parameters
Expand All @@ -244,6 +245,8 @@ def text(src, yx, text, size, color=(0, 0, 0)):
color: (3,) array-like
Text RGB color in uint8.
Default is (0, 0, 0), which is black.
font_path: str
Default font is DejaVuSansMono in matplotlib.
Returns
-------
Expand All @@ -256,7 +259,7 @@ def text(src, yx, text, size, color=(0, 0, 0)):

y1, x1 = yx
color = tuple(color)
font = _get_font(size=size)
font = _get_font(size=size, font_path=font_path)
draw.text(xy=(x1, y1), text=text, fill=color, font=font)

return np.array(dst)
Expand Down
16 changes: 14 additions & 2 deletions imgviz/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def label2rgb(
thresh_suppress=0,
colormap=None,
loc="centroid",
font_path=None,
):
"""Convert label to rgb.
Expand All @@ -80,6 +81,8 @@ def label2rgb(
loc: string
Location of legend (default: 'centroid').
'lt' and 'rb' are supported.
font_path: str
Font path.
Returns
-------
Expand Down Expand Up @@ -122,21 +125,29 @@ def label2rgb(
y, x = Y[point_index], X[point_index]

text = label_names[label_i]
height, width = draw_module.text_size(text, size=font_size)
height, width = draw_module.text_size(
text, size=font_size, font_path=font_path
)
color = color_module.get_fg_color(res[y, x])
res = draw_module.text(
res,
yx=(y - height // 2, x - width // 2),
text=text,
color=color,
size=font_size,
font_path=font_path,
)
elif loc in ["rb", "lt"]:
unique_labels = np.unique(label)
unique_labels = unique_labels[unique_labels != -1]
if not unique_labels.size:
return res

text_sizes = np.array(
[
draw_module.text_size(label_names[l], font_size)
draw_module.text_size(
label_names[l], font_size, font_path=font_path
)
for l in unique_labels
]
)
Expand Down Expand Up @@ -176,6 +187,7 @@ def label2rgb(
yx=aabb1 + (i * text_height, 30),
text=label_names[l],
size=font_size,
font_path=font_path,
)
else:
raise ValueError("unsupported loc: {}".format(loc))
Expand Down

0 comments on commit efcea27

Please sign in to comment.