Spaces:
Sleeping
Sleeping
update service
Browse files- app/services/video_service.py +373 -392
app/services/video_service.py
CHANGED
@@ -1,393 +1,374 @@
|
|
1 |
-
import cv2
|
2 |
-
import numpy as np
|
3 |
-
from PIL import Image, ImageDraw, ImageFont
|
4 |
-
import os
|
5 |
-
import logging
|
6 |
-
import base64
|
7 |
-
from app.services.font_manager import FontManager
|
8 |
-
import io
|
9 |
-
from moviepy.editor import VideoFileClip, AudioFileClip, CompositeVideoClip
|
10 |
-
from moviepy.audio.AudioClip import CompositeAudioClip
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
'
|
23 |
-
'
|
24 |
-
'
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
#
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
#
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
corner_radius =
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
#
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
lines = []
|
375 |
-
current_line = []
|
376 |
-
|
377 |
-
for word in words:
|
378 |
-
current_line.append(word)
|
379 |
-
line = ' '.join(current_line)
|
380 |
-
bbox = font.getbbox(line)
|
381 |
-
if bbox[2] > max_width:
|
382 |
-
if len(current_line) == 1:
|
383 |
-
lines.append(line)
|
384 |
-
current_line = []
|
385 |
-
else:
|
386 |
-
current_line.pop()
|
387 |
-
lines.append(' '.join(current_line))
|
388 |
-
current_line = [word]
|
389 |
-
|
390 |
-
if current_line:
|
391 |
-
lines.append(' '.join(current_line))
|
392 |
-
|
393 |
return '\n'.join(lines)
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image, ImageDraw, ImageFont
|
4 |
+
import os
|
5 |
+
import logging
|
6 |
+
import base64
|
7 |
+
from app.services.font_manager import FontManager
|
8 |
+
import io
|
9 |
+
from moviepy.editor import VideoFileClip, AudioFileClip, CompositeVideoClip
|
10 |
+
from moviepy.audio.AudioClip import CompositeAudioClip
|
11 |
+
from io import BytesIO
|
12 |
+
from fastapi.responses import StreamingResponse
|
13 |
+
|
14 |
+
logger = logging.getLogger(__name__)
|
15 |
+
|
16 |
+
class VideoService:
|
17 |
+
# Initialiser le gestionnaire de polices
|
18 |
+
font_manager = FontManager()
|
19 |
+
|
20 |
+
# Constantes de style
|
21 |
+
COLORS = {
|
22 |
+
'background': (25, 25, 25),
|
23 |
+
'text': (255, 255, 255),
|
24 |
+
'highlight': (64, 156, 255),
|
25 |
+
'correct': (46, 204, 113),
|
26 |
+
'option_bg': (50, 50, 50)
|
27 |
+
}
|
28 |
+
|
29 |
+
@staticmethod
|
30 |
+
async def generate_quiz_video(quiz_data: dict):
|
31 |
+
try:
|
32 |
+
# Configuration
|
33 |
+
WIDTH, HEIGHT = 720, 1280
|
34 |
+
FPS = 24
|
35 |
+
DURATION_QUESTION = 5
|
36 |
+
DURATION_ANSWER = 3
|
37 |
+
|
38 |
+
# Récupérer les styles depuis quiz_data
|
39 |
+
style_config = quiz_data.get('styleConfig', {})
|
40 |
+
title_style = style_config.get('title', {})
|
41 |
+
questions_style = style_config.get('questions', {})
|
42 |
+
answers_style = style_config.get('answers', {})
|
43 |
+
background_style = style_config.get('background', {})
|
44 |
+
|
45 |
+
# Créer un buffer en mémoire au lieu d'un fichier
|
46 |
+
video_buffer = BytesIO()
|
47 |
+
|
48 |
+
# Créer le writer avec cv2
|
49 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
50 |
+
out = cv2.VideoWriter('temp.mp4', fourcc, FPS, (WIDTH, HEIGHT))
|
51 |
+
|
52 |
+
# Charger l'image de fond si elle existe
|
53 |
+
background_image = None
|
54 |
+
if background_style.get('image'):
|
55 |
+
# Décoder l'image base64 en gardant les couleurs d'origine
|
56 |
+
image_data = base64.b64decode(background_style['image'].split(',')[1])
|
57 |
+
img = Image.open(io.BytesIO(image_data))
|
58 |
+
|
59 |
+
# Redimensionner en conservant le ratio
|
60 |
+
ratio = img.width / img.height
|
61 |
+
new_height = HEIGHT
|
62 |
+
new_width = int(HEIGHT * ratio)
|
63 |
+
img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
64 |
+
|
65 |
+
# Centrer et recadrer si nécessaire
|
66 |
+
if new_width > WIDTH:
|
67 |
+
left = (new_width - WIDTH) // 2
|
68 |
+
img = img.crop((left, 0, left + WIDTH, HEIGHT))
|
69 |
+
elif new_width < WIDTH:
|
70 |
+
new_img = Image.new('RGB', (WIDTH, HEIGHT), (0, 0, 0))
|
71 |
+
paste_x = (WIDTH - new_width) // 2
|
72 |
+
new_img.paste(img, (paste_x, 0))
|
73 |
+
img = new_img
|
74 |
+
|
75 |
+
# Convertir en array numpy en préservant les couleurs
|
76 |
+
background_image = np.array(img)
|
77 |
+
|
78 |
+
# Liste pour stocker les moments où jouer le son
|
79 |
+
correct_answer_times = []
|
80 |
+
current_time = 0
|
81 |
+
|
82 |
+
# Création des frames
|
83 |
+
for i, question in enumerate(quiz_data["questions"], 1):
|
84 |
+
frame = Image.new('RGB', (WIDTH, HEIGHT))
|
85 |
+
if background_image is not None:
|
86 |
+
# Utiliser l'image de fond en RGB
|
87 |
+
frame = Image.fromarray(background_image)
|
88 |
+
if background_style.get('opacity', 1) < 1:
|
89 |
+
overlay = Image.new('RGB', (WIDTH, HEIGHT), (0, 0, 0))
|
90 |
+
frame = Image.blend(frame, overlay, 1 - background_style.get('opacity', 1))
|
91 |
+
|
92 |
+
# Créer les frames
|
93 |
+
question_frame = VideoService._create_question_frame(
|
94 |
+
frame, question, i, len(quiz_data["questions"]),
|
95 |
+
title_style, questions_style, answers_style,
|
96 |
+
WIDTH, HEIGHT, show_answer=False
|
97 |
+
)
|
98 |
+
|
99 |
+
# Convertir en BGR pour OpenCV
|
100 |
+
frame_cv = cv2.cvtColor(np.array(question_frame), cv2.COLOR_RGB2BGR)
|
101 |
+
|
102 |
+
for _ in range(int(FPS * DURATION_QUESTION)):
|
103 |
+
out.write(frame_cv)
|
104 |
+
current_time += DURATION_QUESTION
|
105 |
+
|
106 |
+
# Marquer le moment pour jouer le son
|
107 |
+
correct_answer_times.append(current_time)
|
108 |
+
|
109 |
+
# Frame de réponse
|
110 |
+
answer_frame = VideoService._create_question_frame(
|
111 |
+
frame.copy(), question, i, len(quiz_data["questions"]),
|
112 |
+
title_style, questions_style, answers_style,
|
113 |
+
WIDTH, HEIGHT, show_answer=True
|
114 |
+
)
|
115 |
+
|
116 |
+
frame_cv = cv2.cvtColor(np.array(answer_frame), cv2.COLOR_RGB2BGR)
|
117 |
+
|
118 |
+
for _ in range(int(FPS * DURATION_ANSWER)):
|
119 |
+
out.write(frame_cv)
|
120 |
+
current_time += DURATION_ANSWER
|
121 |
+
|
122 |
+
out.release()
|
123 |
+
|
124 |
+
# Lire le fichier temporaire en mémoire
|
125 |
+
with open('temp.mp4', 'rb') as f:
|
126 |
+
video_buffer.write(f.read())
|
127 |
+
|
128 |
+
# Supprimer le fichier temporaire
|
129 |
+
os.remove('temp.mp4')
|
130 |
+
|
131 |
+
# Remettre le curseur au début du buffer
|
132 |
+
video_buffer.seek(0)
|
133 |
+
|
134 |
+
# Retourner le stream
|
135 |
+
return StreamingResponse(
|
136 |
+
video_buffer,
|
137 |
+
media_type="video/mp4",
|
138 |
+
headers={
|
139 |
+
'Content-Disposition': f'attachment; filename="quiz_{quiz_data["id"]}.mp4"'
|
140 |
+
}
|
141 |
+
)
|
142 |
+
|
143 |
+
except Exception as e:
|
144 |
+
logger.error(f"Erreur dans generate_quiz_video: {str(e)}")
|
145 |
+
raise
|
146 |
+
|
147 |
+
@staticmethod
|
148 |
+
def _scale_size(size, preview_height=170, video_height=720):
|
149 |
+
"""
|
150 |
+
Convertit une taille de la preview vers la taille vidéo ou inverse
|
151 |
+
en conservant le ratio
|
152 |
+
"""
|
153 |
+
scale_factor = video_height / preview_height
|
154 |
+
return int(size * scale_factor)
|
155 |
+
|
156 |
+
@staticmethod
|
157 |
+
def _create_question_frame(frame, question, current_num, total_questions,
|
158 |
+
title_style, questions_style, answers_style,
|
159 |
+
width, height, show_answer=False):
|
160 |
+
draw = ImageDraw.Draw(frame)
|
161 |
+
|
162 |
+
try:
|
163 |
+
# Définir les tailles de base pour la preview (170px)
|
164 |
+
BASE_PREVIEW_HEIGHT = 170
|
165 |
+
|
166 |
+
# Convertir les tailles de police de la preview vers la taille vidéo
|
167 |
+
title_base_size = int(title_style.get('fontSize', 11)) # taille en px pour preview
|
168 |
+
question_base_size = int(questions_style.get('fontSize', 8))
|
169 |
+
answer_base_size = int(answers_style.get('fontSize', 6))
|
170 |
+
|
171 |
+
# Mettre à l'échelle pour la vidéo
|
172 |
+
title_font_size = VideoService._scale_size(title_base_size)
|
173 |
+
question_font_size = VideoService._scale_size(question_base_size)
|
174 |
+
answer_font_size = VideoService._scale_size(answer_base_size)
|
175 |
+
|
176 |
+
title_font = ImageFont.truetype(
|
177 |
+
VideoService.font_manager.get_font_path(title_style.get('fontFamily')),
|
178 |
+
title_font_size
|
179 |
+
)
|
180 |
+
question_font = ImageFont.truetype(
|
181 |
+
VideoService.font_manager.get_font_path(questions_style.get('fontFamily')),
|
182 |
+
question_font_size
|
183 |
+
)
|
184 |
+
answer_font = ImageFont.truetype(
|
185 |
+
VideoService.font_manager.get_font_path(answers_style.get('fontFamily')),
|
186 |
+
answer_font_size
|
187 |
+
)
|
188 |
+
|
189 |
+
# Position du titre à 10% du haut
|
190 |
+
title_y = int(0.10 * height)
|
191 |
+
title_text = f"Question {current_num}/{total_questions}"
|
192 |
+
VideoService._draw_text(draw, title_text, title_font, title_y, width, title_style)
|
193 |
+
|
194 |
+
# Position de la question à 10% en dessous du titre
|
195 |
+
question_y = int(0.23 * height) # 10% + 10%
|
196 |
+
question_text = VideoService._wrap_text(question['question'], question_font, width - 100)
|
197 |
+
VideoService._draw_text(draw, question_text, question_font, question_y, width, questions_style)
|
198 |
+
|
199 |
+
# Position des réponses à 10% en dessous de la question
|
200 |
+
start_y = int(0.38 * height)
|
201 |
+
spacing_between_options = int(0.12 * height) # Espacement entre les centres des blocs
|
202 |
+
|
203 |
+
# Pré-calculer les hauteurs des blocs
|
204 |
+
option_heights = []
|
205 |
+
for option in question['options']:
|
206 |
+
letter = chr(65 + len(option_heights))
|
207 |
+
full_text = f"{letter}. {option}"
|
208 |
+
wrapped_text = VideoService._wrap_text(full_text, answer_font, width - (width * 0.1 + 50))
|
209 |
+
lines = wrapped_text.split('\n')
|
210 |
+
|
211 |
+
bbox = answer_font.getbbox('Ag')
|
212 |
+
line_height = bbox[3] - bbox[1]
|
213 |
+
text_height = line_height * len(lines)
|
214 |
+
actual_height = max(80, text_height + 40) # même calcul que dans _draw_option
|
215 |
+
option_heights.append(actual_height)
|
216 |
+
|
217 |
+
# Dessiner chaque option en tenant compte des hauteurs
|
218 |
+
current_y = start_y
|
219 |
+
for i, option in enumerate(question['options']):
|
220 |
+
is_correct = show_answer and option == question['correct_answer']
|
221 |
+
VideoService._draw_option(draw, option, current_y,
|
222 |
+
answer_font, width, answers_style, is_correct, i)
|
223 |
+
|
224 |
+
# Calculer la position du prochain bloc en tenant compte des hauteurs
|
225 |
+
if i < len(question['options']) - 1:
|
226 |
+
current_block_half = option_heights[i] / 2
|
227 |
+
next_block_half = option_heights[i + 1] / 2
|
228 |
+
current_y += spacing_between_options # Espacement fixe entre les centres
|
229 |
+
|
230 |
+
return frame
|
231 |
+
|
232 |
+
except Exception as e:
|
233 |
+
logger.error(f"Erreur dans _create_question_frame: {str(e)}")
|
234 |
+
raise
|
235 |
+
|
236 |
+
@staticmethod
|
237 |
+
def _draw_option(draw, option_text, y_position, font, width, style, is_correct=False, option_index=0):
|
238 |
+
# Hauteur minimale du bloc d'option
|
239 |
+
option_height = 80
|
240 |
+
margin_left = width * 0.1
|
241 |
+
|
242 |
+
# Calculer la hauteur réelle du texte
|
243 |
+
letter = chr(65 + option_index)
|
244 |
+
full_text = f"{letter}. {option_text}"
|
245 |
+
wrapped_text = VideoService._wrap_text(full_text, font, width - (margin_left + 50))
|
246 |
+
lines = wrapped_text.split('\n')
|
247 |
+
|
248 |
+
# Calculer la hauteur totale du texte
|
249 |
+
bbox = font.getbbox('Ag') # Utiliser une ligne de référence pour la hauteur
|
250 |
+
line_height = bbox[3] - bbox[1]
|
251 |
+
text_height = line_height * len(lines)
|
252 |
+
|
253 |
+
# Utiliser la plus grande valeur entre option_height et text_height
|
254 |
+
actual_height = max(option_height, text_height + 40) # +40 pour le padding
|
255 |
+
|
256 |
+
# Dessiner le fond
|
257 |
+
if style.get('backgroundColor'):
|
258 |
+
bg_color = tuple(int(style['backgroundColor'][i:i+2], 16) for i in (1, 3, 5))
|
259 |
+
draw.rectangle(
|
260 |
+
[
|
261 |
+
(50, y_position - actual_height//2),
|
262 |
+
(width - 50, y_position + actual_height//2)
|
263 |
+
],
|
264 |
+
fill=bg_color
|
265 |
+
)
|
266 |
+
|
267 |
+
# Si c'est la bonne réponse, on force la couleur en vert
|
268 |
+
if is_correct:
|
269 |
+
style = style.copy()
|
270 |
+
style['color'] = '#2ECC71'
|
271 |
+
|
272 |
+
# Dessiner le texte aligné à gauche avec la marge
|
273 |
+
VideoService._draw_text(draw, wrapped_text, font, y_position, width, style, align_left=True, margin_left=margin_left)
|
274 |
+
|
275 |
+
@staticmethod
|
276 |
+
def _draw_text(draw, text, font, y_position, width, style, align_left=False, margin_left=0):
|
277 |
+
try:
|
278 |
+
lines = text.split('\n')
|
279 |
+
|
280 |
+
# Calculer la hauteur totale avec plus d'espacement entre les lignes
|
281 |
+
line_heights = []
|
282 |
+
line_widths = []
|
283 |
+
total_height = 0
|
284 |
+
max_width = 0
|
285 |
+
line_spacing = 1 # Facteur d'espacement entre les lignes (1.5 fois la hauteur normale)
|
286 |
+
|
287 |
+
for line in lines:
|
288 |
+
bbox = font.getbbox(line)
|
289 |
+
line_height = bbox[3] - bbox[1]
|
290 |
+
line_width = bbox[2] - bbox[0]
|
291 |
+
|
292 |
+
line_heights.append(line_height)
|
293 |
+
line_widths.append(line_width)
|
294 |
+
total_height += line_height * line_spacing # Multiplier par le facteur d'espacement
|
295 |
+
max_width = max(max_width, line_width)
|
296 |
+
|
297 |
+
# Augmenter le padding autour du texte
|
298 |
+
padding = 20 # Augmenté de 20 à 30
|
299 |
+
|
300 |
+
current_y = y_position - (total_height // 2)
|
301 |
+
|
302 |
+
if style.get('backgroundColor'):
|
303 |
+
corner_radius = 15
|
304 |
+
bg_color = tuple(int(style['backgroundColor'][i:i+2], 16) for i in (1, 3, 5))
|
305 |
+
|
306 |
+
if align_left:
|
307 |
+
x1, y1 = 50, current_y - padding
|
308 |
+
x2, y2 = width - 50, current_y + total_height + padding
|
309 |
+
else:
|
310 |
+
center_x = width // 2
|
311 |
+
x1 = center_x - (max_width // 2) - padding
|
312 |
+
x2 = center_x + (max_width // 2) + padding
|
313 |
+
y1 = current_y - padding
|
314 |
+
y2 = current_y + total_height + padding
|
315 |
+
|
316 |
+
# Dessiner un rectangle avec coins arrondis
|
317 |
+
draw.pieslice([x1, y1, x1 + corner_radius * 2, y1 + corner_radius * 2], 180, 270, fill=bg_color)
|
318 |
+
draw.pieslice([x2 - corner_radius * 2, y1, x2, y1 + corner_radius * 2], 270, 0, fill=bg_color)
|
319 |
+
draw.pieslice([x1, y2 - corner_radius * 2, x1 + corner_radius * 2, y2], 90, 180, fill=bg_color)
|
320 |
+
draw.pieslice([x2 - corner_radius * 2, y2 - corner_radius * 2, x2, y2], 0, 90, fill=bg_color)
|
321 |
+
|
322 |
+
draw.rectangle([x1 + corner_radius, y1, x2 - corner_radius, y2], fill=bg_color)
|
323 |
+
draw.rectangle([x1, y1 + corner_radius, x2, y2 - corner_radius], fill=bg_color)
|
324 |
+
|
325 |
+
# Dessiner chaque ligne de texte avec plus d'espacement
|
326 |
+
for i, line in enumerate(lines):
|
327 |
+
if align_left:
|
328 |
+
x_position = margin_left
|
329 |
+
else:
|
330 |
+
x_position = (width - line_widths[i]) // 2
|
331 |
+
|
332 |
+
color = tuple(int(style.get('color', '#FFFFFF')[i:i+2], 16) for i in (1, 3, 5))
|
333 |
+
# Assurer une épaisseur minimale de 1 pour le contour
|
334 |
+
stroke_width = max(1, 8*int(float(style.get('textStrokeWidth', 0))))
|
335 |
+
stroke_color = tuple(int(style.get('textStrokeColor', '#000000')[i:i+2], 16) for i in (1, 3, 5))
|
336 |
+
|
337 |
+
if float(style.get('textStrokeWidth', 0)) > 0: # Vérifier la valeur originale
|
338 |
+
# Dessiner d'abord le contour
|
339 |
+
draw.text((x_position, current_y), line,
|
340 |
+
font=font, fill=stroke_color, stroke_width=stroke_width)
|
341 |
+
|
342 |
+
# Dessiner le texte principal
|
343 |
+
draw.text((x_position, current_y), line,
|
344 |
+
font=font, fill=color)
|
345 |
+
|
346 |
+
current_y += line_heights[i] * line_spacing # Multiplier par le facteur d'espacement
|
347 |
+
|
348 |
+
except Exception as e:
|
349 |
+
logger.error(f"Erreur dans _draw_text: {str(e)}")
|
350 |
+
raise
|
351 |
+
|
352 |
+
@staticmethod
|
353 |
+
def _wrap_text(text: str, font: ImageFont, max_width: int) -> str:
|
354 |
+
words = text.split()
|
355 |
+
lines = []
|
356 |
+
current_line = []
|
357 |
+
|
358 |
+
for word in words:
|
359 |
+
current_line.append(word)
|
360 |
+
line = ' '.join(current_line)
|
361 |
+
bbox = font.getbbox(line)
|
362 |
+
if bbox[2] > max_width:
|
363 |
+
if len(current_line) == 1:
|
364 |
+
lines.append(line)
|
365 |
+
current_line = []
|
366 |
+
else:
|
367 |
+
current_line.pop()
|
368 |
+
lines.append(' '.join(current_line))
|
369 |
+
current_line = [word]
|
370 |
+
|
371 |
+
if current_line:
|
372 |
+
lines.append(' '.join(current_line))
|
373 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
374 |
return '\n'.join(lines)
|