habulaj commited on
Commit
29db2e8
·
verified ·
1 Parent(s): 561450d

Update routers/memoriam.py

Browse files
Files changed (1) hide show
  1. routers/memoriam.py +48 -14
routers/memoriam.py CHANGED
@@ -58,8 +58,7 @@ def draw_single_line_text_centered(draw: ImageDraw.Draw, text: str, max_width: i
58
 
59
  text_width = draw.textlength(text, font=font)
60
  if text_width <= max_width:
61
- # Centraliza o texto e aplica kerning manual
62
- spacing = -0.03 * font_size # -3% espaçamento entre letras
63
  draw.text(
64
  (center_x - text_width / 2, y),
65
  text,
@@ -69,9 +68,34 @@ def draw_single_line_text_centered(draw: ImageDraw.Draw, text: str, max_width: i
69
  )
70
  break
71
 
72
- def create_canvas(image_url: Optional[str], headline: Optional[str]) -> BytesIO:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  width, height = 1080, 1350
74
- canvas = Image.new("RGBA", (width, height), (0, 0, 0, 0)) # Fundo transparente
75
 
76
  if image_url:
77
  img = download_image_from_url(image_url)
@@ -82,7 +106,7 @@ def create_canvas(image_url: Optional[str], headline: Optional[str]) -> BytesIO:
82
  gradient_overlay = create_linear_black_gradient(width, height)
83
  canvas = Image.alpha_composite(canvas, gradient_overlay)
84
 
85
- # Adicionar logo
86
  try:
87
  logo = Image.open("recurve.png").convert("RGBA")
88
  logo_resized = logo.resize((int(148.36), int(27.9)))
@@ -90,14 +114,24 @@ def create_canvas(image_url: Optional[str], headline: Optional[str]) -> BytesIO:
90
  except Exception as e:
91
  raise HTTPException(status_code=500, detail=f"Erro ao carregar a logo: {e}")
92
 
93
- # Adicionar título
94
- if headline:
95
- draw = ImageDraw.Draw(canvas)
 
96
  font_path = "fonts/Montserrat-Bold.ttf"
97
- max_width = 947
98
- center_x = width // 2
99
- y = 1100
100
- draw_single_line_text_centered(draw, headline, max_width, center_x, y, font_path)
 
 
 
 
 
 
 
 
 
101
 
102
  buffer = BytesIO()
103
  canvas.save(buffer, format="PNG")
@@ -107,10 +141,10 @@ def create_canvas(image_url: Optional[str], headline: Optional[str]) -> BytesIO:
107
  @router.get("/memoriam")
108
  def get_memoriam_image(
109
  image_url: Optional[str] = Query(None, description="URL da imagem de fundo"),
110
- headline: Optional[str] = Query(None, description="Texto do título (opcional)")
111
  ):
112
  try:
113
- buffer = create_canvas(image_url, headline)
114
  return StreamingResponse(buffer, media_type="image/png")
115
  except Exception as e:
116
  raise HTTPException(status_code=500, detail=f"Erro ao gerar imagem: {str(e)}")
 
58
 
59
  text_width = draw.textlength(text, font=font)
60
  if text_width <= max_width:
61
+ spacing = -0.03 * font_size
 
62
  draw.text(
63
  (center_x - text_width / 2, y),
64
  text,
 
68
  )
69
  break
70
 
71
+ def create_gradient_bar(width: int, height: int) -> Image.Image:
72
+ bar = Image.new("RGBA", (width, height))
73
+ draw = ImageDraw.Draw(bar)
74
+
75
+ for x in range(width):
76
+ ratio = x / (width - 1)
77
+ if ratio < 0.5:
78
+ r = int(0xFF * (1 - ratio * 2) + 0xF4 * ratio * 2)
79
+ g = int(0x82 * (1 - ratio * 2) + 0x0A * ratio * 2)
80
+ b = int(0x26 * (1 - ratio * 2) + 0xFF * ratio * 2)
81
+ else:
82
+ ratio2 = (ratio - 0.5) * 2
83
+ r = int(0xF4 * (1 - ratio2) + 0x03 * ratio2)
84
+ g = int(0x0A * (1 - ratio2) + 0xD9 * ratio2)
85
+ b = int(0xFF * (1 - ratio2) + 0xE3 * ratio2)
86
+ draw.line([(x, 0), (x, height)], fill=(r, g, b, 255))
87
+
88
+ # Arredondar cantos
89
+ mask = Image.new("L", (width, height), 0)
90
+ mask_draw = ImageDraw.Draw(mask)
91
+ mask_draw.rounded_rectangle([0, 0, width, height], radius=100, fill=255)
92
+ bar.putalpha(mask)
93
+
94
+ return bar
95
+
96
+ def create_canvas(image_url: Optional[str], name: Optional[str]) -> BytesIO:
97
  width, height = 1080, 1350
98
+ canvas = Image.new("RGBA", (width, height), (0, 0, 0, 0))
99
 
100
  if image_url:
101
  img = download_image_from_url(image_url)
 
106
  gradient_overlay = create_linear_black_gradient(width, height)
107
  canvas = Image.alpha_composite(canvas, gradient_overlay)
108
 
109
+ # Logo
110
  try:
111
  logo = Image.open("recurve.png").convert("RGBA")
112
  logo_resized = logo.resize((int(148.36), int(27.9)))
 
114
  except Exception as e:
115
  raise HTTPException(status_code=500, detail=f"Erro ao carregar a logo: {e}")
116
 
117
+ draw = ImageDraw.Draw(canvas)
118
+
119
+ # Texto (name)
120
+ if name:
121
  font_path = "fonts/Montserrat-Bold.ttf"
122
+ name_upper = name.upper()
123
+ draw_single_line_text_centered(
124
+ draw,
125
+ text=name_upper,
126
+ max_width=947,
127
+ center_x=width // 2,
128
+ y=1100,
129
+ font_path=font_path
130
+ )
131
+
132
+ # Retângulo degradê com cantos arredondados
133
+ bar = create_gradient_bar(110, int(6.82))
134
+ canvas.paste(bar, (480, int(1213)), bar)
135
 
136
  buffer = BytesIO()
137
  canvas.save(buffer, format="PNG")
 
141
  @router.get("/memoriam")
142
  def get_memoriam_image(
143
  image_url: Optional[str] = Query(None, description="URL da imagem de fundo"),
144
+ name: Optional[str] = Query(None, description="Nome a ser exibido (em maiúsculas)")
145
  ):
146
  try:
147
+ buffer = create_canvas(image_url, name)
148
  return StreamingResponse(buffer, media_type="image/png")
149
  except Exception as e:
150
  raise HTTPException(status_code=500, detail=f"Erro ao gerar imagem: {str(e)}")