Spaces:
Running
Running
Update routers/video.py
Browse files- routers/video.py +37 -19
routers/video.py
CHANGED
|
@@ -14,14 +14,17 @@ def render_headline_text(draw: ImageDraw.Draw, text: str, x: int, y: int, max_wi
|
|
| 14 |
Args:
|
| 15 |
draw: Objeto ImageDraw para desenhar
|
| 16 |
text: Texto do headline
|
| 17 |
-
x: Posição X
|
| 18 |
-
y: Posição Y
|
| 19 |
max_width: Largura máxima do texto
|
| 20 |
max_lines: Número máximo de linhas (padrão: 5)
|
| 21 |
"""
|
| 22 |
if not text.strip():
|
| 23 |
return
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
# Carregar fonte Montserrat-ExtraBold
|
| 26 |
try:
|
| 27 |
font_path = "fonts/Montserrat-ExtraBold.ttf"
|
|
@@ -105,18 +108,24 @@ def render_headline_text(draw: ImageDraw.Draw, text: str, x: int, y: int, max_wi
|
|
| 105 |
line_height = bbox[3] - bbox[1]
|
| 106 |
line_heights.append(line_height)
|
| 107 |
|
| 108 |
-
# Calcular posição inicial (Y é a posição da
|
|
|
|
| 109 |
current_y = y
|
| 110 |
|
| 111 |
-
# Desenhar cada linha
|
| 112 |
-
for i, line in enumerate(lines):
|
| 113 |
-
line_height = line_heights[i]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
# Desenhar texto principal (branco)
|
| 116 |
-
draw.text((
|
| 117 |
|
| 118 |
-
# Atualizar posição para próxima linha
|
| 119 |
-
current_y
|
| 120 |
|
| 121 |
def render_headline_text_white(draw: ImageDraw.Draw, text: str, x: int, y: int, max_width: int, max_lines: int = 5) -> None:
|
| 122 |
"""
|
|
@@ -125,6 +134,9 @@ def render_headline_text_white(draw: ImageDraw.Draw, text: str, x: int, y: int,
|
|
| 125 |
if not text.strip():
|
| 126 |
return
|
| 127 |
|
|
|
|
|
|
|
|
|
|
| 128 |
# Carregar fonte Montserrat-ExtraBold
|
| 129 |
try:
|
| 130 |
font_path = "fonts/Montserrat-ExtraBold.ttf"
|
|
@@ -198,18 +210,24 @@ def render_headline_text_white(draw: ImageDraw.Draw, text: str, x: int, y: int,
|
|
| 198 |
line_height = bbox[3] - bbox[1]
|
| 199 |
line_heights.append(line_height)
|
| 200 |
|
| 201 |
-
# Calcular posição inicial
|
|
|
|
| 202 |
current_y = y
|
| 203 |
|
| 204 |
-
# Desenhar cada linha (branco)
|
| 205 |
-
for i, line in enumerate(lines):
|
| 206 |
-
line_height = line_heights[i]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
# Desenhar texto principal (branco)
|
| 209 |
-
draw.text((
|
| 210 |
|
| 211 |
-
# Atualizar posição para próxima linha
|
| 212 |
-
current_y
|
| 213 |
|
| 214 |
@router.get("/cover/video")
|
| 215 |
def get_video_cover(
|
|
@@ -276,8 +294,8 @@ def get_video_cover(
|
|
| 276 |
render_headline_text(
|
| 277 |
text_draw,
|
| 278 |
headline,
|
| 279 |
-
x=
|
| 280 |
-
y=
|
| 281 |
max_width=720,
|
| 282 |
max_lines=5
|
| 283 |
)
|
|
@@ -298,7 +316,7 @@ def get_video_cover(
|
|
| 298 |
text_white_draw = ImageDraw.Draw(text_white_img)
|
| 299 |
|
| 300 |
# Renderizar texto branco
|
| 301 |
-
render_headline_text_white(text_white_draw, headline,
|
| 302 |
|
| 303 |
# Combinar texto principal
|
| 304 |
background_image = Image.alpha_composite(background_image, text_white_img)
|
|
|
|
| 14 |
Args:
|
| 15 |
draw: Objeto ImageDraw para desenhar
|
| 16 |
text: Texto do headline
|
| 17 |
+
x: Posição X (centro)
|
| 18 |
+
y: Posição Y (base - texto alinhado de baixo para cima)
|
| 19 |
max_width: Largura máxima do texto
|
| 20 |
max_lines: Número máximo de linhas (padrão: 5)
|
| 21 |
"""
|
| 22 |
if not text.strip():
|
| 23 |
return
|
| 24 |
|
| 25 |
+
# Converter texto para maiúsculas
|
| 26 |
+
text = text.upper()
|
| 27 |
+
|
| 28 |
# Carregar fonte Montserrat-ExtraBold
|
| 29 |
try:
|
| 30 |
font_path = "fonts/Montserrat-ExtraBold.ttf"
|
|
|
|
| 108 |
line_height = bbox[3] - bbox[1]
|
| 109 |
line_heights.append(line_height)
|
| 110 |
|
| 111 |
+
# Calcular posição inicial (Y é a posição da base - última linha)
|
| 112 |
+
# Começar de baixo para cima
|
| 113 |
current_y = y
|
| 114 |
|
| 115 |
+
# Desenhar cada linha (de baixo para cima)
|
| 116 |
+
for i, line in enumerate(reversed(lines)):
|
| 117 |
+
line_height = line_heights[len(lines) - 1 - i]
|
| 118 |
+
|
| 119 |
+
# Calcular posição X centralizada para esta linha
|
| 120 |
+
bbox = draw.textbbox((0, 0), line, font=final_font)
|
| 121 |
+
line_width = bbox[2] - bbox[0]
|
| 122 |
+
centered_x = x - (line_width // 2)
|
| 123 |
|
| 124 |
# Desenhar texto principal (branco)
|
| 125 |
+
draw.text((centered_x, current_y), line, fill=(0, 0, 0, 89), font=final_font) # Texto com opacidade para sombra
|
| 126 |
|
| 127 |
+
# Atualizar posição para próxima linha (subindo)
|
| 128 |
+
current_y -= int(line_height * 1.2) # Line height de 120%
|
| 129 |
|
| 130 |
def render_headline_text_white(draw: ImageDraw.Draw, text: str, x: int, y: int, max_width: int, max_lines: int = 5) -> None:
|
| 131 |
"""
|
|
|
|
| 134 |
if not text.strip():
|
| 135 |
return
|
| 136 |
|
| 137 |
+
# Converter texto para maiúsculas
|
| 138 |
+
text = text.upper()
|
| 139 |
+
|
| 140 |
# Carregar fonte Montserrat-ExtraBold
|
| 141 |
try:
|
| 142 |
font_path = "fonts/Montserrat-ExtraBold.ttf"
|
|
|
|
| 210 |
line_height = bbox[3] - bbox[1]
|
| 211 |
line_heights.append(line_height)
|
| 212 |
|
| 213 |
+
# Calcular posição inicial (Y é a posição da base - última linha)
|
| 214 |
+
# Começar de baixo para cima
|
| 215 |
current_y = y
|
| 216 |
|
| 217 |
+
# Desenhar cada linha (branco) - de baixo para cima
|
| 218 |
+
for i, line in enumerate(reversed(lines)):
|
| 219 |
+
line_height = line_heights[len(lines) - 1 - i]
|
| 220 |
+
|
| 221 |
+
# Calcular posição X centralizada para esta linha
|
| 222 |
+
bbox = draw.textbbox((0, 0), line, font=final_font)
|
| 223 |
+
line_width = bbox[2] - bbox[0]
|
| 224 |
+
centered_x = x - (line_width // 2)
|
| 225 |
|
| 226 |
# Desenhar texto principal (branco)
|
| 227 |
+
draw.text((centered_x, current_y), line, fill=(255, 255, 255, 255), font=final_font)
|
| 228 |
|
| 229 |
+
# Atualizar posição para próxima linha (subindo)
|
| 230 |
+
current_y -= int(line_height * 1.2)
|
| 231 |
|
| 232 |
@router.get("/cover/video")
|
| 233 |
def get_video_cover(
|
|
|
|
| 294 |
render_headline_text(
|
| 295 |
text_draw,
|
| 296 |
headline,
|
| 297 |
+
x=540, # Centro da imagem (1080/2)
|
| 298 |
+
y=1300,
|
| 299 |
max_width=720,
|
| 300 |
max_lines=5
|
| 301 |
)
|
|
|
|
| 316 |
text_white_draw = ImageDraw.Draw(text_white_img)
|
| 317 |
|
| 318 |
# Renderizar texto branco
|
| 319 |
+
render_headline_text_white(text_white_draw, headline, 540, 1300, 720, 5)
|
| 320 |
|
| 321 |
# Combinar texto principal
|
| 322 |
background_image = Image.alpha_composite(background_image, text_white_img)
|