certificate / app.py
maringetxway's picture
Update app.py
38bccdf verified
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()