georgescutelnicu commited on
Commit
a19db3b
·
verified ·
1 Parent(s): 5e85145

Delete add_text.py

Browse files
Files changed (1) hide show
  1. add_text.py +0 -54
add_text.py DELETED
@@ -1,54 +0,0 @@
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
- wrapped_text = textwrap.fill(text, width=int(w * 0.1), break_long_words=True)
26
-
27
- line_height = 12
28
- font_size = 10
29
- font = ImageFont.truetype(font_path, size=font_size)
30
-
31
- lines = wrapped_text.split('\n')
32
- total_text_height = (len(lines)) * line_height
33
-
34
- if total_text_height > h:
35
- font_size *= (h / total_text_height)
36
- line_height = 10
37
- total_text_height = (len(lines)) * line_height
38
-
39
- # Vertical centering
40
- text_y = y + (h - total_text_height) // 2
41
-
42
- for line in lines:
43
- text_length = draw.textlength(line, font=font)
44
-
45
- # Horizontal centering
46
- text_x = x + (w - text_length) // 2
47
-
48
- draw.text((text_x, text_y), line, font=font, fill=(0, 0, 0))
49
-
50
- text_y += line_height
51
-
52
- image[:, :, :] = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
53
-
54
- return image