Update routers/memoriam.py
Browse files- routers/memoriam.py +43 -20
routers/memoriam.py
CHANGED
@@ -84,22 +84,41 @@ def create_gradient_bar(width: int, height: int, radius: int = 50) -> Image.Imag
|
|
84 |
b = int(0xFF * (1 - ratio2) + 0xE3 * ratio2)
|
85 |
draw.line([(x, 0), (x, height)], fill=(r, g, b, 255))
|
86 |
|
87 |
-
#
|
88 |
mask = Image.new("L", (width, height), 0)
|
89 |
mask_draw = ImageDraw.Draw(mask)
|
90 |
mask_draw.rounded_rectangle([(0, 0), (width, height)], radius=radius, fill=255)
|
91 |
gradient.putalpha(mask)
|
92 |
return gradient
|
93 |
|
94 |
-
def
|
95 |
try:
|
96 |
font = ImageFont.truetype(font_path, 24)
|
97 |
except Exception:
|
98 |
font = ImageFont.load_default()
|
99 |
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
def create_canvas(image_url: Optional[str], name: Optional[str], birth: Optional[str], death: Optional[str]) -> BytesIO:
|
105 |
width, height = 1080, 1350
|
@@ -133,7 +152,7 @@ def create_canvas(image_url: Optional[str], name: Optional[str], birth: Optional
|
|
133 |
y = 1100
|
134 |
draw_single_line_text_centered(draw, name_upper, max_width, center_x, y, font_path)
|
135 |
|
136 |
-
# Barra
|
137 |
bar_x = 480
|
138 |
bar_y = int(1213)
|
139 |
bar_width = 110
|
@@ -141,23 +160,27 @@ def create_canvas(image_url: Optional[str], name: Optional[str], birth: Optional
|
|
141 |
bar = create_gradient_bar(bar_width, bar_height, radius=50)
|
142 |
canvas.paste(bar, (bar_x, bar_y), bar)
|
143 |
|
144 |
-
#
|
145 |
text_y = bar_y + (bar_height - 28) // 2
|
146 |
-
|
|
|
|
|
147 |
|
148 |
if birth:
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
|
158 |
if death:
|
159 |
-
|
160 |
-
|
|
|
|
|
161 |
|
162 |
buffer = BytesIO()
|
163 |
canvas.save(buffer, format="PNG")
|
@@ -168,8 +191,8 @@ def create_canvas(image_url: Optional[str], name: Optional[str], birth: Optional
|
|
168 |
def get_memoriam_image(
|
169 |
image_url: Optional[str] = Query(None, description="URL da imagem de fundo"),
|
170 |
name: Optional[str] = Query(None, description="Nome (será exibido em maiúsculas)"),
|
171 |
-
birth: Optional[str] = Query(None, description="Ano de nascimento"),
|
172 |
-
death: Optional[str] = Query(None, description="Ano de falecimento")
|
173 |
):
|
174 |
try:
|
175 |
buffer = create_canvas(image_url, name, birth, death)
|
|
|
84 |
b = int(0xFF * (1 - ratio2) + 0xE3 * ratio2)
|
85 |
draw.line([(x, 0), (x, height)], fill=(r, g, b, 255))
|
86 |
|
87 |
+
# Bordas arredondadas
|
88 |
mask = Image.new("L", (width, height), 0)
|
89 |
mask_draw = ImageDraw.Draw(mask)
|
90 |
mask_draw.rounded_rectangle([(0, 0), (width, height)], radius=radius, fill=255)
|
91 |
gradient.putalpha(mask)
|
92 |
return gradient
|
93 |
|
94 |
+
def draw_year_with_icon(draw: ImageDraw.Draw, base_image: Image.Image, text: str, icon_path: str, x: int, y: int, font_path: str, icon_width: int):
|
95 |
try:
|
96 |
font = ImageFont.truetype(font_path, 24)
|
97 |
except Exception:
|
98 |
font = ImageFont.load_default()
|
99 |
|
100 |
+
spacing_between_icon_and_text = 8
|
101 |
+
text_width = draw.textlength(text, font=font)
|
102 |
+
|
103 |
+
# Carregar e redimensionar o ícone mantendo proporção da altura igual a altura do texto (aprox 28px)
|
104 |
+
icon = Image.open(icon_path).convert("RGBA")
|
105 |
+
icon_ratio = icon.width / icon.height
|
106 |
+
icon_height = 28 # altura aproximada para alinhamento com o texto (altura da linha)
|
107 |
+
icon_resized = icon.resize((icon_width, int(icon_width / icon_ratio))) if icon_width < icon.height else icon.resize((int(icon_height * icon_ratio), icon_height))
|
108 |
+
|
109 |
+
# Ajuste: usar icon_height fixa para alinhamento vertical (ícones diferentes podem ter alturas diferentes)
|
110 |
+
icon_resized = icon.resize((icon_width, icon_height))
|
111 |
+
|
112 |
+
# Y para alinhar verticalmente ícone e texto
|
113 |
+
icon_y = y
|
114 |
+
text_y = y
|
115 |
+
|
116 |
+
# Colar ícone na base_image
|
117 |
+
base_image.paste(icon_resized, (x, icon_y), icon_resized)
|
118 |
+
|
119 |
+
# Desenhar texto após o ícone + espaçamento
|
120 |
+
text_x = x + icon_width + spacing_between_icon_and_text
|
121 |
+
draw.text((text_x, text_y), text, font=font, fill=(255, 255, 255), spacing=0)
|
122 |
|
123 |
def create_canvas(image_url: Optional[str], name: Optional[str], birth: Optional[str], death: Optional[str]) -> BytesIO:
|
124 |
width, height = 1080, 1350
|
|
|
152 |
y = 1100
|
153 |
draw_single_line_text_centered(draw, name_upper, max_width, center_x, y, font_path)
|
154 |
|
155 |
+
# Barra gradiente
|
156 |
bar_x = 480
|
157 |
bar_y = int(1213)
|
158 |
bar_width = 110
|
|
|
160 |
bar = create_gradient_bar(bar_width, bar_height, radius=50)
|
161 |
canvas.paste(bar, (bar_x, bar_y), bar)
|
162 |
|
163 |
+
# Altura da linha do texto para alinhamento vertical dos ícones e textos
|
164 |
text_y = bar_y + (bar_height - 28) // 2
|
165 |
+
|
166 |
+
# Desenhar anos com ícones
|
167 |
+
font_path_semi = "fonts/Montserrat-SemiBold.ttf"
|
168 |
|
169 |
if birth:
|
170 |
+
# calcular largura do texto para posicionar o ícone e o texto lado a lado
|
171 |
+
# posicionar a estrela + texto, sendo o x do texto após o ícone + espaçamento
|
172 |
+
# A estrela deve ficar 12px à esquerda da barra
|
173 |
+
icon_width_star = 30
|
174 |
+
# Ajusta x do ícone para ficar a 12px à esquerda do início da barra menos largura do texto e espaço do ícone+texto
|
175 |
+
text_width = draw.textlength(birth, font=ImageFont.truetype(font_path_semi, 24))
|
176 |
+
icon_x = bar_x - 12 - icon_width_star - 8 - int(text_width) # 8px espaço ícone-texto
|
177 |
+
draw_year_with_icon(draw, canvas, birth, "star.png", icon_x, text_y, font_path_semi, icon_width_star)
|
178 |
|
179 |
if death:
|
180 |
+
icon_width_cross = 18
|
181 |
+
# Ícone + 8px + texto começa a 12px depois da barra
|
182 |
+
icon_x = bar_x + bar_width + 12
|
183 |
+
draw_year_with_icon(draw, canvas, death, "cross.png", icon_x, text_y, font_path_semi, icon_width_cross)
|
184 |
|
185 |
buffer = BytesIO()
|
186 |
canvas.save(buffer, format="PNG")
|
|
|
191 |
def get_memoriam_image(
|
192 |
image_url: Optional[str] = Query(None, description="URL da imagem de fundo"),
|
193 |
name: Optional[str] = Query(None, description="Nome (será exibido em maiúsculas)"),
|
194 |
+
birth: Optional[str] = Query(None, description="Ano de nascimento (ex: 1943)"),
|
195 |
+
death: Optional[str] = Query(None, description="Ano de falecimento (ex: 2023)")
|
196 |
):
|
197 |
try:
|
198 |
buffer = create_canvas(image_url, name, birth, death)
|