Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -313,14 +313,26 @@ class CertificateGenerator:
|
|
| 313 |
return fonts
|
| 314 |
|
| 315 |
def _add_professional_border(self, draw: ImageDraw.Draw):
|
| 316 |
-
|
| 317 |
padding = 40
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
|
| 325 |
def _add_content(
|
| 326 |
self,
|
|
@@ -376,23 +388,27 @@ class CertificateGenerator:
|
|
| 376 |
print(f"Error adding logo: {e}")
|
| 377 |
|
| 378 |
def _add_photo(self, certificate: Image.Image, photo_path: str):
|
| 379 |
-
"""Add a circular profile photo
|
| 380 |
try:
|
| 381 |
-
# Open and
|
| 382 |
photo = Image.open(photo_path)
|
| 383 |
|
| 384 |
-
# Define size for circular photo
|
| 385 |
-
size = (120, 120)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
|
| 387 |
-
# Create circular mask
|
| 388 |
mask = Image.new('L', size, 0)
|
| 389 |
draw = ImageDraw.Draw(mask)
|
| 390 |
draw.ellipse((0, 0, size[0], size[1]), fill=255)
|
| 391 |
|
| 392 |
-
# Resize photo maintaining aspect ratio
|
| 393 |
aspect = photo.width / photo.height
|
| 394 |
if aspect > 1:
|
| 395 |
-
# Width is greater
|
| 396 |
new_height = size[1]
|
| 397 |
new_width = int(new_height * aspect)
|
| 398 |
photo = photo.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
|
@@ -400,7 +416,7 @@ class CertificateGenerator:
|
|
| 400 |
left = (new_width - size[0]) // 2
|
| 401 |
photo = photo.crop((left, 0, left + size[0], size[1]))
|
| 402 |
else:
|
| 403 |
-
# Height is greater
|
| 404 |
new_width = size[0]
|
| 405 |
new_height = int(new_width / aspect)
|
| 406 |
photo = photo.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
|
@@ -411,17 +427,28 @@ class CertificateGenerator:
|
|
| 411 |
# Create output image with transparency
|
| 412 |
output = Image.new('RGBA', size, (0, 0, 0, 0))
|
| 413 |
output.paste(photo, (0, 0))
|
|
|
|
|
|
|
|
|
|
| 414 |
output.putalpha(mask)
|
| 415 |
|
| 416 |
-
#
|
| 417 |
-
|
| 418 |
-
|
|
|
|
| 419 |
|
| 420 |
-
#
|
| 421 |
-
|
|
|
|
|
|
|
| 422 |
|
| 423 |
-
|
| 424 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 425 |
|
| 426 |
def generate(
|
| 427 |
self,
|
|
@@ -471,7 +498,21 @@ class CertificateGenerator:
|
|
| 471 |
return None
|
| 472 |
|
| 473 |
def _create_base_certificate(self) -> Image.Image:
|
| 474 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 475 |
|
| 476 |
def _save_certificate(self, certificate: Image.Image) -> str:
|
| 477 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
|
|
|
|
| 313 |
return fonts
|
| 314 |
|
| 315 |
def _add_professional_border(self, draw: ImageDraw.Draw):
|
| 316 |
+
"""Add professional border with improved corners"""
|
| 317 |
padding = 40
|
| 318 |
+
border_width = 2
|
| 319 |
+
corner_radius = 10
|
| 320 |
+
|
| 321 |
+
# Draw rounded rectangle border
|
| 322 |
+
x0, y0 = padding, padding
|
| 323 |
+
x1, y1 = self.certificate_size[0] - padding, self.certificate_size[1] - padding
|
| 324 |
+
|
| 325 |
+
# Draw corners
|
| 326 |
+
draw.arc((x0, y0, x0 + corner_radius * 2, y0 + corner_radius * 2), 180, 270, '#1C1D1F', border_width)
|
| 327 |
+
draw.arc((x1 - corner_radius * 2, y0, x1, y0 + corner_radius * 2), 270, 0, '#1C1D1F', border_width)
|
| 328 |
+
draw.arc((x0, y1 - corner_radius * 2, x0 + corner_radius * 2, y1), 90, 180, '#1C1D1F', border_width)
|
| 329 |
+
draw.arc((x1 - corner_radius * 2, y1 - corner_radius * 2, x1, y1), 0, 90, '#1C1D1F', border_width)
|
| 330 |
+
|
| 331 |
+
# Draw lines
|
| 332 |
+
draw.line((x0 + corner_radius, y0, x1 - corner_radius, y0), '#1C1D1F', border_width) # Top
|
| 333 |
+
draw.line((x0 + corner_radius, y1, x1 - corner_radius, y1), '#1C1D1F', border_width) # Bottom
|
| 334 |
+
draw.line((x0, y0 + corner_radius, x0, y1 - corner_radius), '#1C1D1F', border_width) # Left
|
| 335 |
+
draw.line((x1, y0 + corner_radius, x1, y1 - corner_radius), '#1C1D1F', border_width) # Right
|
| 336 |
|
| 337 |
def _add_content(
|
| 338 |
self,
|
|
|
|
| 388 |
print(f"Error adding logo: {e}")
|
| 389 |
|
| 390 |
def _add_photo(self, certificate: Image.Image, photo_path: str):
|
| 391 |
+
"""Add a clear circular profile photo in the top-right corner"""
|
| 392 |
try:
|
| 393 |
+
# Open and process photo
|
| 394 |
photo = Image.open(photo_path)
|
| 395 |
|
| 396 |
+
# Define size for circular photo (slightly larger for better quality)
|
| 397 |
+
size = (120, 120)
|
| 398 |
+
|
| 399 |
+
# Convert to RGBA if not already
|
| 400 |
+
if photo.mode != 'RGBA':
|
| 401 |
+
photo = photo.convert('RGBA')
|
| 402 |
|
| 403 |
+
# Create high-quality circular mask
|
| 404 |
mask = Image.new('L', size, 0)
|
| 405 |
draw = ImageDraw.Draw(mask)
|
| 406 |
draw.ellipse((0, 0, size[0], size[1]), fill=255)
|
| 407 |
|
| 408 |
+
# Resize photo maintaining aspect ratio
|
| 409 |
aspect = photo.width / photo.height
|
| 410 |
if aspect > 1:
|
| 411 |
+
# Width is greater
|
| 412 |
new_height = size[1]
|
| 413 |
new_width = int(new_height * aspect)
|
| 414 |
photo = photo.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
|
|
|
| 416 |
left = (new_width - size[0]) // 2
|
| 417 |
photo = photo.crop((left, 0, left + size[0], size[1]))
|
| 418 |
else:
|
| 419 |
+
# Height is greater
|
| 420 |
new_width = size[0]
|
| 421 |
new_height = int(new_width / aspect)
|
| 422 |
photo = photo.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
|
|
|
| 427 |
# Create output image with transparency
|
| 428 |
output = Image.new('RGBA', size, (0, 0, 0, 0))
|
| 429 |
output.paste(photo, (0, 0))
|
| 430 |
+
|
| 431 |
+
# Apply antialiasing to the mask
|
| 432 |
+
mask = mask.filter(ImageFilter.GaussianBlur(radius=1))
|
| 433 |
output.putalpha(mask)
|
| 434 |
|
| 435 |
+
# Position in top-right corner with padding
|
| 436 |
+
padding_right = 60
|
| 437 |
+
padding_top = 40
|
| 438 |
+
position = (certificate.width - size[0] - padding_right, padding_top)
|
| 439 |
|
| 440 |
+
# Add a subtle white background circle for better visibility
|
| 441 |
+
bg = Image.new('RGBA', size, (255, 255, 255, 255))
|
| 442 |
+
bg_mask = mask.copy()
|
| 443 |
+
certificate.paste(bg, position, mask=bg_mask)
|
| 444 |
|
| 445 |
+
# Paste the photo
|
| 446 |
+
certificate.paste(output, position, mask=output)
|
| 447 |
+
|
| 448 |
+
except Exception as e:
|
| 449 |
+
print(f"Error adding photo: {str(e)}")
|
| 450 |
+
print(f"Photo path: {photo_path}")
|
| 451 |
+
print(f"Photo details: {getattr(photo, 'size', 'unknown size')}, {getattr(photo, 'mode', 'unknown mode')}")
|
| 452 |
|
| 453 |
def generate(
|
| 454 |
self,
|
|
|
|
| 498 |
return None
|
| 499 |
|
| 500 |
def _create_base_certificate(self) -> Image.Image:
|
| 501 |
+
"""Create base certificate with improved background"""
|
| 502 |
+
# Create base image
|
| 503 |
+
certificate = Image.new('RGB', self.certificate_size, self.background_color)
|
| 504 |
+
|
| 505 |
+
# Add subtle gradient background (optional)
|
| 506 |
+
draw = ImageDraw.Draw(certificate)
|
| 507 |
+
|
| 508 |
+
# Add very subtle grain texture for professional look (optional)
|
| 509 |
+
width, height = certificate.size
|
| 510 |
+
for x in range(0, width, 4):
|
| 511 |
+
for y in range(0, height, 4):
|
| 512 |
+
if random.random() > 0.5:
|
| 513 |
+
draw.point((x, y), fill=(250, 250, 250))
|
| 514 |
+
|
| 515 |
+
return certificate
|
| 516 |
|
| 517 |
def _save_certificate(self, certificate: Image.Image) -> str:
|
| 518 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
|