File size: 1,782 Bytes
768b0b9 38bccdf 768b0b9 |
1 2 3 4 5 6 7 8 9 10 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 |
import gradio as gr
from PIL import Image, ImageDraw, ImageFont
def generate_certificate(name):
# Load certificate template (you can replace this with your own image)
certificate = Image.open("certificate_template.png")
draw = ImageDraw.Draw(certificate)
# Use a nice script font (Luxurious Script or similar)
try:
font = ImageFont.truetype("LuxuriousScript-Regular.ttf", 150) # Ensure you have the font file
except IOError:
font = ImageFont.load_default() # Fallback font if the specific one is not found
# Get the bounding box of the text to calculate its width and height
bbox = draw.textbbox((0, 0), name, font=font) # Get the bounding box of the text
text_width = bbox[2] - bbox[0] # Width from bbox
text_height = bbox[3] - bbox[1] # Height from bbox
# Calculate position to center the name
position = ((certificate.width - text_width) // 2, (certificate.height - text_height) // 2)
# Add the participant's name to the certificate in the center
draw.text(position, name, font=font, fill="black")
# Save the generated certificate with the name
certificate.save(f"{name}_certificate.png")
# Return the image to be displayed and downloaded in the Gradio interface
return certificate, f"{name}_certificate.png"
# Define the Gradio interface
iface = gr.Interface(
fn=generate_certificate,
inputs=gr.Textbox(label="Enter Your Name"),
outputs=[gr.Image(type="pil"), gr.File(label="Download Certificate")], # Added file output for download
title="π Generate your Certificate for participation",
description="Enter your name below to generate your personalized certificate.",
live=True
)
# Launch the interface
iface.launch()
|