|
| 1 | +"""Label primitive.""" |
| 2 | + |
| 3 | +# Copyright (C) 2025 Intel Corporation |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +from io import BytesIO |
| 7 | +from typing import Union |
| 8 | + |
| 9 | +from PIL import Image, ImageDraw, ImageFont |
| 10 | + |
| 11 | +from .primitive import Primitive |
| 12 | + |
| 13 | + |
| 14 | +class Label(Primitive): |
| 15 | + """Label primitive. |
| 16 | +
|
| 17 | + Labels require a different processing than other primitives as the class also handles the instance when the layout |
| 18 | + requests all the labels to be drawn on a single image. |
| 19 | +
|
| 20 | + Args: |
| 21 | + label (str): Text of the label. |
| 22 | + fg_color (str | tuple[int, int, int]): Foreground color of the label. |
| 23 | + bg_color (str | tuple[int, int, int]): Background color of the label. |
| 24 | + font_path (str | None | BytesIO): Path to the font file. |
| 25 | + size (int): Size of the font. |
| 26 | +
|
| 27 | + Examples: |
| 28 | + >>> label = Label(label="Label 1") |
| 29 | + >>> label.compute(image).save("label.jpg") |
| 30 | +
|
| 31 | + >>> label = Label(text="Label 1", fg_color="red", bg_color="blue", font_path="arial.ttf", size=20) |
| 32 | + >>> label.compute(image).save("label.jpg") |
| 33 | +
|
| 34 | + or multiple labels on a single image: |
| 35 | + >>> label1 = Label(text="Label 1") |
| 36 | + >>> label2 = Label(text="Label 2") |
| 37 | + >>> label3 = Label(text="Label 3") |
| 38 | + >>> Label.overlay_labels(image, [label1, label2, label3]).save("labels.jpg") |
| 39 | + """ |
| 40 | + |
| 41 | + def __init__( |
| 42 | + self, |
| 43 | + label: str, |
| 44 | + fg_color: Union[str, tuple[int, int, int]] = "black", |
| 45 | + bg_color: Union[str, tuple[int, int, int]] = "yellow", |
| 46 | + font_path: Union[str, BytesIO, None] = None, |
| 47 | + size: int = 16, |
| 48 | + ) -> None: |
| 49 | + self.label = label |
| 50 | + self.fg_color = fg_color |
| 51 | + self.bg_color = bg_color |
| 52 | + self.font = ImageFont.load_default(size=size) if font_path is None else ImageFont.truetype(font_path, size) |
| 53 | + |
| 54 | + def compute(self, image: Image, buffer_y: int = 5) -> Image: |
| 55 | + """Generate label on top of the image. |
| 56 | +
|
| 57 | + Args: |
| 58 | + image (PIL.Image): Image to paste the label on. |
| 59 | + buffer_y (int): Buffer to add to the y-axis of the label. |
| 60 | + """ |
| 61 | + label_image = self.generate_label_image(buffer_y) |
| 62 | + image.paste(label_image, (0, 0)) |
| 63 | + return image |
| 64 | + |
| 65 | + def generate_label_image(self, buffer_y: int = 5) -> Image: |
| 66 | + """Generate label image. |
| 67 | +
|
| 68 | + Args: |
| 69 | + buffer_y (int): Buffer to add to the y-axis of the label. This is needed as the text is clipped from the |
| 70 | + bottom otherwise. |
| 71 | +
|
| 72 | + Returns: |
| 73 | + PIL.Image: Image that consists only of the label. |
| 74 | + """ |
| 75 | + dummy_image = Image.new("RGB", (1, 1)) |
| 76 | + draw = ImageDraw.Draw(dummy_image) |
| 77 | + textbox = draw.textbbox((0, 0), self.label, font=self.font) |
| 78 | + label_image = Image.new("RGB", (textbox[2] - textbox[0], textbox[3] + buffer_y - textbox[1]), self.bg_color) |
| 79 | + draw = ImageDraw.Draw(label_image) |
| 80 | + draw.text((0, 0), self.label, font=self.font, fill=self.fg_color) |
| 81 | + return label_image |
| 82 | + |
| 83 | + @classmethod |
| 84 | + def overlay_labels(cls, image: Image, labels: list["Label"], buffer_y: int = 5, buffer_x: int = 5) -> Image: |
| 85 | + """Overlay multiple label images on top of the image. |
| 86 | + Paste the labels in a row but wrap the labels if they exceed the image width. |
| 87 | +
|
| 88 | + Args: |
| 89 | + image (PIL.Image): Image to paste the labels on. |
| 90 | + labels (list[Label]): Labels to be pasted on the image. |
| 91 | + buffer_y (int): Buffer to add to the y-axis of the labels. |
| 92 | + buffer_x (int): Space between the labels. |
| 93 | +
|
| 94 | + Returns: |
| 95 | + PIL.Image: Image with the labels pasted on it. |
| 96 | + """ |
| 97 | + offset_x = 0 |
| 98 | + offset_y = 0 |
| 99 | + for label in labels: |
| 100 | + label_image = label.generate_label_image(buffer_y) |
| 101 | + image.paste(label_image, (offset_x, offset_y)) |
| 102 | + offset_x += label_image.width + buffer_x |
| 103 | + if offset_x + label_image.width > image.width: |
| 104 | + offset_x = 0 |
| 105 | + offset_y += label_image.height |
| 106 | + return image |
0 commit comments