Draw a black label on a white background with no border.
(
text: str,
font: Path,
font_size: int,
device: torch.device = torch.device("cpu"),
)
| 238 | |
| 239 | |
| 240 | def 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 | |
| 262 | def add_label( |