maggidev commited on
Commit
7981cc4
·
verified ·
1 Parent(s): d65402d

Update add_text.py

Browse files
Files changed (1) hide show
  1. add_text.py +24 -18
add_text.py CHANGED
@@ -7,69 +7,75 @@ import numpy as np
7
  def add_text(
8
  image,
9
  text,
10
- bbox,
11
  font_path,
12
  initial_font_size=42,
13
  padding=10,
14
  line_spacing=1.25
15
  ):
16
  """
17
- Insere texto dentro de uma bolha respeitando largura, altura e estética tipográfica.
18
- Prioriza quebra de linhas antes de reduzir drasticamente o tamanho da fonte.
19
  """
20
 
21
- x, y, w, h = bbox
 
 
22
  img_pil = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
23
  draw = ImageDraw.Draw(img_pil)
24
 
25
  font_size = initial_font_size
26
- wrapping_ratio = 0.9 # começa permitindo múltiplas linhas
27
 
28
  while font_size > 10:
29
  font = ImageFont.truetype(font_path, font_size)
30
 
31
- max_chars_per_line = max(1, int((w - 2 * padding) / (font_size * wrapping_ratio)))
 
 
 
32
  lines = textwrap.wrap(text, width=max_chars_per_line)
33
 
34
- # --- REGRA CRÍTICA: NÃO PERMITIR LINHA ÚNICA SE HOUVER ALTURA ---
35
  min_lines = 2 if h > font_size * 2.5 else 1
36
  if len(lines) < min_lines:
37
  wrapping_ratio -= 0.03
38
  continue
39
 
40
- # Medições reais
41
- line_heights = []
42
  max_line_width = 0
 
43
 
44
  for line in lines:
45
  bbox_text = draw.textbbox((0, 0), line, font=font)
46
  lw = bbox_text[2] - bbox_text[0]
47
  lh = bbox_text[3] - bbox_text[1]
48
  max_line_width = max(max_line_width, lw)
49
- line_heights.append(lh)
50
 
51
- total_height = int(sum(line_heights) * line_spacing)
52
 
53
- # --- VERIFICA ENCAIXE ---
54
- if total_height <= (h - 2 * padding) and max_line_width <= (w - 2 * padding):
 
 
55
  break
56
 
57
- # --- PRIORIDADE CORRETA ---
58
  if max_line_width > (w - 2 * padding):
59
- wrapping_ratio -= 0.02 # quebra mais linhas
60
  else:
61
- font_size -= 1 # só reduz fonte se necessário
62
 
63
- # --- DESENHO FINAL ---
64
  current_y = y + padding + (h - total_height) // 2
65
 
66
  for line in lines:
67
  bbox_text = draw.textbbox((0, 0), line, font=font)
68
  lw = bbox_text[2] - bbox_text[0]
 
69
 
70
  text_x = x + (w - lw) // 2
71
  draw.text((text_x, current_y), line, fill=(0, 0, 0), font=font)
72
 
73
- current_y += int((bbox_text[3] - bbox_text[1]) * line_spacing)
74
 
75
  return cv2.cvtColor(np.array(img_pil), cv2.COLOR_RGB2BGR)
 
7
  def add_text(
8
  image,
9
  text,
10
+ contour,
11
  font_path,
12
  initial_font_size=42,
13
  padding=10,
14
  line_spacing=1.25
15
  ):
16
  """
17
+ Renderiza texto dentro de uma bolha usando o CONTOUR como referência.
18
+ Prioriza quebra de linhas antes de reduzir a fonte.
19
  """
20
 
21
+ # --- Extrai bounding box do contorno ---
22
+ x, y, w, h = cv2.boundingRect(contour)
23
+
24
  img_pil = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
25
  draw = ImageDraw.Draw(img_pil)
26
 
27
  font_size = initial_font_size
28
+ wrapping_ratio = 0.9
29
 
30
  while font_size > 10:
31
  font = ImageFont.truetype(font_path, font_size)
32
 
33
+ max_chars_per_line = max(
34
+ 1, int((w - 2 * padding) / (font_size * wrapping_ratio))
35
+ )
36
+
37
  lines = textwrap.wrap(text, width=max_chars_per_line)
38
 
39
+ # Evita linha única quando espaço vertical
40
  min_lines = 2 if h > font_size * 2.5 else 1
41
  if len(lines) < min_lines:
42
  wrapping_ratio -= 0.03
43
  continue
44
 
 
 
45
  max_line_width = 0
46
+ total_height = 0
47
 
48
  for line in lines:
49
  bbox_text = draw.textbbox((0, 0), line, font=font)
50
  lw = bbox_text[2] - bbox_text[0]
51
  lh = bbox_text[3] - bbox_text[1]
52
  max_line_width = max(max_line_width, lw)
53
+ total_height += lh
54
 
55
+ total_height = int(total_height * line_spacing)
56
 
57
+ if (
58
+ total_height <= (h - 2 * padding)
59
+ and max_line_width <= (w - 2 * padding)
60
+ ):
61
  break
62
 
 
63
  if max_line_width > (w - 2 * padding):
64
+ wrapping_ratio -= 0.02
65
  else:
66
+ font_size -= 1
67
 
68
+ # --- Centralização ---
69
  current_y = y + padding + (h - total_height) // 2
70
 
71
  for line in lines:
72
  bbox_text = draw.textbbox((0, 0), line, font=font)
73
  lw = bbox_text[2] - bbox_text[0]
74
+ lh = bbox_text[3] - bbox_text[1]
75
 
76
  text_x = x + (w - lw) // 2
77
  draw.text((text_x, current_y), line, fill=(0, 0, 0), font=font)
78
 
79
+ current_y += int(lh * line_spacing)
80
 
81
  return cv2.cvtColor(np.array(img_pil), cv2.COLOR_RGB2BGR)