MCPcopy Create free account
hub / github.com/dcharatan/flowmap / draw_label

Function draw_label

flowmap/visualization/layout.py:240–259  ·  view source on GitHub ↗

Draw a black label on a white background with no border.

(
    text: str,
    font: Path,
    font_size: int,
    device: torch.device = torch.device("cpu"),
)

Source from the content-addressed store, hash-verified

238
239
240def draw_label(
241 text: str,
242 font: Path,
243 font_size: int,
244 device: torch.device = torch.device("cpu"),
245) -> Float[Tensor, "3 height width"]:
246 """Draw a black label on a white background with no border."""
247 try:
248 font = ImageFont.truetype(str(font), font_size)
249 except OSError:
250 font = ImageFont.load_default()
251 left, _, right, _ = font.getbbox(text)
252 width = right - left
253 _, top, _, bottom = font.getbbox(EXPECTED_CHARACTERS)
254 height = bottom - top
255 image = Image.new("RGB", (width, height), color="white")
256 draw = ImageDraw.Draw(image)
257 draw.text((0, 0), text, font=font, fill="black")
258 image = torch.tensor(np.array(image) / 255, dtype=torch.float32, device=device)
259 return rearrange(image, "h w c -> c h w")
260
261
262def add_label(

Callers 1

add_labelFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected