Spaces:
Sleeping
Sleeping
Update add_text.py
Browse files- add_text.py +29 -44
add_text.py
CHANGED
|
@@ -1,65 +1,50 @@
|
|
| 1 |
from PIL import Image, ImageDraw, ImageFont
|
| 2 |
import numpy as np
|
| 3 |
-
import textwrap
|
| 4 |
import cv2
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
def add_text(image, text, font_path, bubble_contour):
|
| 8 |
-
"""
|
| 9 |
-
Add text inside a speech bubble contour.
|
| 10 |
-
|
| 11 |
-
Args:
|
| 12 |
-
image (numpy.ndarray): Processed bubble image (cv2 format - BGR).
|
| 13 |
-
text (str): Text to be placed inside the speech bubble.
|
| 14 |
-
font_path (str): Font path.
|
| 15 |
-
bubble_contour (numpy.ndarray): Contour of the detected speech bubble.
|
| 16 |
-
|
| 17 |
-
Returns:
|
| 18 |
-
numpy.ndarray: Image with text placed inside the speech bubble.
|
| 19 |
-
"""
|
| 20 |
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
| 21 |
draw = ImageDraw.Draw(pil_image)
|
| 22 |
|
| 23 |
x, y, w, h = cv2.boundingRect(bubble_contour)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
wrapping_ratio = 0.
|
| 28 |
|
| 29 |
-
|
| 30 |
-
break_long_words=True)
|
| 31 |
-
|
| 32 |
-
font = ImageFont.truetype(font_path, size=font_size)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
font_size -= 2
|
| 40 |
-
wrapping_ratio += 0.025
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
text_y = y + (h - total_text_height) // 2
|
| 52 |
|
| 53 |
for line in lines:
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
text_x = x + (w - text_length) // 2
|
| 58 |
-
|
| 59 |
-
draw.text((text_x, text_y), line, font=font, fill=(0, 0, 0))
|
| 60 |
-
|
| 61 |
text_y += line_height
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
return image
|
|
|
|
| 1 |
from PIL import Image, ImageDraw, ImageFont
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
import cv2
|
| 4 |
+
import textwrap
|
| 5 |
|
| 6 |
|
| 7 |
def add_text(image, text, font_path, bubble_contour):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
| 9 |
draw = ImageDraw.Draw(pil_image)
|
| 10 |
|
| 11 |
x, y, w, h = cv2.boundingRect(bubble_contour)
|
| 12 |
|
| 13 |
+
font_size = max(12, int(h * 0.08))
|
| 14 |
+
line_height = int(font_size * 1.2)
|
| 15 |
+
wrapping_ratio = 0.09
|
| 16 |
|
| 17 |
+
font = ImageFont.truetype(font_path, font_size)
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
wrapped_text = textwrap.fill(
|
| 20 |
+
text,
|
| 21 |
+
width=max(1, int(w * wrapping_ratio)),
|
| 22 |
+
break_long_words=True
|
| 23 |
+
)
|
| 24 |
|
| 25 |
+
lines = wrapped_text.split("\n")
|
| 26 |
+
total_height = len(lines) * line_height
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
while total_height > h and font_size > 10:
|
| 29 |
+
font_size -= 2
|
| 30 |
+
line_height = int(font_size * 1.2)
|
| 31 |
+
wrapping_ratio += 0.02
|
| 32 |
+
font = ImageFont.truetype(font_path, font_size)
|
| 33 |
|
| 34 |
+
wrapped_text = textwrap.fill(
|
| 35 |
+
text,
|
| 36 |
+
width=max(1, int(w * wrapping_ratio)),
|
| 37 |
+
break_long_words=True
|
| 38 |
+
)
|
| 39 |
+
lines = wrapped_text.split("\n")
|
| 40 |
+
total_height = len(lines) * line_height
|
| 41 |
|
| 42 |
+
text_y = y + (h - total_height) // 2
|
|
|
|
| 43 |
|
| 44 |
for line in lines:
|
| 45 |
+
text_width = draw.textlength(line, font=font)
|
| 46 |
+
text_x = x + (w - text_width) // 2
|
| 47 |
+
draw.text((text_x, text_y), line, fill=(0, 0, 0), font=font)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
text_y += line_height
|
| 49 |
|
| 50 |
+
return cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
|
|
|
|
|
|