Spaces:
Running
Running
File size: 1,478 Bytes
b902873 efddbeb b902873 efddbeb b902873 efddbeb b902873 efddbeb b902873 efddbeb b902873 efddbeb b902873 efddbeb b902873 efddbeb b902873 efddbeb b902873 efddbeb b902873 efddbeb b902873 efddbeb b902873 efddbeb b902873 efddbeb b902873 efddbeb b902873 efddbeb b902873 efddbeb b902873 efddbeb b902873 efddbeb b902873 efddbeb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
"""
This module contains a function to add text to an image with a bounding box.
"""
import textwrap
from PIL import Image, ImageDraw, ImageFont
import numpy as np
import cv2
def add_text(image: np.ndarray, text: str, contour: np.ndarray):
"""
Add text to an image with a bounding box.
"""
font_path = "./fonts/fonts_animeace_i.ttf"
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
draw = ImageDraw.Draw(pil_image)
x, y, w, h = cv2.boundingRect(contour)
line_height = 16
font_size = 14
wrapping_ratio = 0.075
wrapped_text = textwrap.fill(text, width=int(w * wrapping_ratio),
break_long_words=True)
font = ImageFont.truetype(font_path, size=font_size)
lines = wrapped_text.split('\n')
total_text_height = (len(lines)) * line_height
while total_text_height > h:
line_height -= 2
font_size -= 2
wrapping_ratio += 0.025
wrapped_text = textwrap.fill(text, width=int(w * wrapping_ratio),
break_long_words=True)
font = ImageFont.truetype(font_path, size=font_size)
lines = wrapped_text.split('\n')
total_text_height = (len(lines)) * line_height
# Vertical centering
text_y = y + (h - total_text_height) // 2
for line in lines:
text_length = draw.textlength(line, font=font)
# Horizontal centering
text_x = x + (w - text_length) // 2
draw.text((text_x, text_y), line, font=font, fill=(0, 0, 0))
text_y += line_height
image[:, :, :] = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
|