Spaces:
Sleeping
Sleeping
capradeepgujaran
commited on
Commit
•
0d693b3
1
Parent(s):
be16a8e
Update app.py
Browse files
app.py
CHANGED
@@ -235,65 +235,16 @@ class CertificateGenerator:
|
|
235 |
'name': default
|
236 |
}
|
237 |
return fonts
|
238 |
-
|
239 |
-
def generate(
|
240 |
-
self,
|
241 |
-
score: float,
|
242 |
-
name: str,
|
243 |
-
course_name: str,
|
244 |
-
company_logo: Optional[str] = None,
|
245 |
-
participant_photo: Optional[str] = None
|
246 |
-
) -> str:
|
247 |
-
try:
|
248 |
-
certificate = self._create_base_certificate()
|
249 |
-
draw = ImageDraw.Draw(certificate)
|
250 |
-
|
251 |
-
# Add professional border
|
252 |
-
self._add_professional_border(draw)
|
253 |
-
|
254 |
-
fonts = self._load_fonts()
|
255 |
-
self._add_content(draw, fonts, str(name), str(course_name), float(score))
|
256 |
-
|
257 |
-
if company_logo:
|
258 |
-
self._add_logo(certificate, company_logo)
|
259 |
-
|
260 |
-
if participant_photo:
|
261 |
-
self._add_photo(certificate, participant_photo)
|
262 |
-
|
263 |
-
return self._save_certificate(certificate)
|
264 |
-
except Exception as e:
|
265 |
-
print(f"Error generating certificate: {e}")
|
266 |
-
return None
|
267 |
-
|
268 |
-
def _create_base_certificate(self) -> Image.Image:
|
269 |
-
return Image.new('RGB', self.certificate_size, self.background_color)
|
270 |
|
271 |
def _add_professional_border(self, draw: ImageDraw.Draw):
|
272 |
-
#
|
|
|
273 |
draw.rectangle(
|
274 |
-
[(
|
|
|
275 |
outline='#1C1D1F',
|
276 |
width=2
|
277 |
)
|
278 |
-
|
279 |
-
# Inner border
|
280 |
-
draw.rectangle(
|
281 |
-
[(40, 40), (self.certificate_size[0] - 40, self.certificate_size[1] - 40)],
|
282 |
-
outline='#1C1D1F',
|
283 |
-
width=1
|
284 |
-
)
|
285 |
-
|
286 |
-
# Corner decorations
|
287 |
-
corner_size = 20
|
288 |
-
for x, y in [(20, 20), (self.certificate_size[0] - 20, 20),
|
289 |
-
(20, self.certificate_size[1] - 20),
|
290 |
-
(self.certificate_size[0] - 20, self.certificate_size[1] - 20)]:
|
291 |
-
draw.rectangle(
|
292 |
-
[(x - corner_size, y - corner_size),
|
293 |
-
(x + corner_size, y + corner_size)],
|
294 |
-
outline='#1C1D1F',
|
295 |
-
width=1
|
296 |
-
)
|
297 |
|
298 |
def _add_content(
|
299 |
self,
|
@@ -303,10 +254,6 @@ class CertificateGenerator:
|
|
303 |
course_name: str,
|
304 |
score: float
|
305 |
):
|
306 |
-
# Add certificate ID at top right
|
307 |
-
certificate_id = f"Certificate no: {datetime.now().strftime('%Y%m%d')}-{abs(hash(name)) % 10000:04d}"
|
308 |
-
draw.text((1100, 60), certificate_id, font=fonts['subtitle'], fill='#666666', anchor="ra")
|
309 |
-
|
310 |
# Add "CERTIFICATE OF COMPLETION" text
|
311 |
draw.text((60, 140), "CERTIFICATE OF COMPLETION", font=fonts['subtitle'], fill='#666666')
|
312 |
|
@@ -315,7 +262,6 @@ class CertificateGenerator:
|
|
315 |
text_width = draw.textlength(course_name, fonts['title'])
|
316 |
max_width = self.certificate_size[0] - 120 # Leave margins
|
317 |
if text_width > max_width:
|
318 |
-
# Split into multiple lines if too long
|
319 |
words = course_name.split()
|
320 |
lines = []
|
321 |
current_line = []
|
@@ -354,9 +300,13 @@ class CertificateGenerator:
|
|
354 |
draw.text((300, 500), "Score", font=fonts['subtitle'], fill='#666666')
|
355 |
draw.text((300, 530), f"{float(score):.1f}%", font=fonts['text'], fill='#1C1D1F')
|
356 |
|
357 |
-
#
|
|
|
358 |
ref_number = f"Reference Number: {abs(hash(name + date_str)) % 10000:04d}"
|
359 |
-
|
|
|
|
|
|
|
360 |
|
361 |
def _add_logo(self, certificate: Image.Image, logo_path: str):
|
362 |
try:
|
@@ -390,10 +340,43 @@ class CertificateGenerator:
|
|
390 |
except Exception as e:
|
391 |
print(f"Error adding photo: {e}")
|
392 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
393 |
def _save_certificate(self, certificate: Image.Image) -> str:
|
394 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
|
395 |
certificate.save(temp_file.name, 'PNG', quality=95)
|
396 |
return temp_file.name
|
|
|
397 |
|
398 |
class QuizApp:
|
399 |
def __init__(self, api_key: str):
|
|
|
235 |
'name': default
|
236 |
}
|
237 |
return fonts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
238 |
|
239 |
def _add_professional_border(self, draw: ImageDraw.Draw):
|
240 |
+
# Single elegant inner border with padding
|
241 |
+
padding = 40
|
242 |
draw.rectangle(
|
243 |
+
[(padding, padding),
|
244 |
+
(self.certificate_size[0] - padding, self.certificate_size[1] - padding)],
|
245 |
outline='#1C1D1F',
|
246 |
width=2
|
247 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
248 |
|
249 |
def _add_content(
|
250 |
self,
|
|
|
254 |
course_name: str,
|
255 |
score: float
|
256 |
):
|
|
|
|
|
|
|
|
|
257 |
# Add "CERTIFICATE OF COMPLETION" text
|
258 |
draw.text((60, 140), "CERTIFICATE OF COMPLETION", font=fonts['subtitle'], fill='#666666')
|
259 |
|
|
|
262 |
text_width = draw.textlength(course_name, fonts['title'])
|
263 |
max_width = self.certificate_size[0] - 120 # Leave margins
|
264 |
if text_width > max_width:
|
|
|
265 |
words = course_name.split()
|
266 |
lines = []
|
267 |
current_line = []
|
|
|
300 |
draw.text((300, 500), "Score", font=fonts['subtitle'], fill='#666666')
|
301 |
draw.text((300, 530), f"{float(score):.1f}%", font=fonts['text'], fill='#1C1D1F')
|
302 |
|
303 |
+
# Footer section with certificate number and reference
|
304 |
+
certificate_id = f"Certificate no: {datetime.now().strftime('%Y%m%d')}-{abs(hash(name)) % 10000:04d}"
|
305 |
ref_number = f"Reference Number: {abs(hash(name + date_str)) % 10000:04d}"
|
306 |
+
|
307 |
+
# Draw footer text aligned to left and right
|
308 |
+
draw.text((60, 720), certificate_id, font=fonts['subtitle'], fill='#666666')
|
309 |
+
draw.text((1140, 720), ref_number, font=fonts['subtitle'], fill='#666666', anchor="ra")
|
310 |
|
311 |
def _add_logo(self, certificate: Image.Image, logo_path: str):
|
312 |
try:
|
|
|
340 |
except Exception as e:
|
341 |
print(f"Error adding photo: {e}")
|
342 |
|
343 |
+
def generate(
|
344 |
+
self,
|
345 |
+
score: float,
|
346 |
+
name: str,
|
347 |
+
course_name: str,
|
348 |
+
company_logo: Optional[str] = None,
|
349 |
+
participant_photo: Optional[str] = None
|
350 |
+
) -> str:
|
351 |
+
try:
|
352 |
+
certificate = self._create_base_certificate()
|
353 |
+
draw = ImageDraw.Draw(certificate)
|
354 |
+
|
355 |
+
# Add professional border
|
356 |
+
self._add_professional_border(draw)
|
357 |
+
|
358 |
+
fonts = self._load_fonts()
|
359 |
+
self._add_content(draw, fonts, str(name), str(course_name), float(score))
|
360 |
+
|
361 |
+
if company_logo:
|
362 |
+
self._add_logo(certificate, company_logo)
|
363 |
+
|
364 |
+
if participant_photo:
|
365 |
+
self._add_photo(certificate, participant_photo)
|
366 |
+
|
367 |
+
return self._save_certificate(certificate)
|
368 |
+
except Exception as e:
|
369 |
+
print(f"Error generating certificate: {e}")
|
370 |
+
return None
|
371 |
+
|
372 |
+
def _create_base_certificate(self) -> Image.Image:
|
373 |
+
return Image.new('RGB', self.certificate_size, self.background_color)
|
374 |
+
|
375 |
def _save_certificate(self, certificate: Image.Image) -> str:
|
376 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
|
377 |
certificate.save(temp_file.name, 'PNG', quality=95)
|
378 |
return temp_file.name
|
379 |
+
|
380 |
|
381 |
class QuizApp:
|
382 |
def __init__(self, api_key: str):
|