Upload app (23).py
Browse files- app (23).py +47 -0
app (23).py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 3 |
+
|
| 4 |
+
def generate_certificate(name):
|
| 5 |
+
# Load certificate template (you can replace this with your own image)
|
| 6 |
+
certificate = Image.open("certificate_template.png")
|
| 7 |
+
draw = ImageDraw.Draw(certificate)
|
| 8 |
+
|
| 9 |
+
# Use a nice script font (Luxurious Script or similar)
|
| 10 |
+
try:
|
| 11 |
+
font = ImageFont.truetype("LuxuriousScript-Regular.ttf", 150) # Ensure you have the font file
|
| 12 |
+
except IOError:
|
| 13 |
+
font = ImageFont.load_default() # Fallback font if the specific one is not found
|
| 14 |
+
|
| 15 |
+
# Get the bounding box of the text to calculate its width and height
|
| 16 |
+
bbox = draw.textbbox((0, 0), name, font=font) # Get the bounding box of the text
|
| 17 |
+
text_width = bbox[2] - bbox[0] # Width from bbox
|
| 18 |
+
text_height = bbox[3] - bbox[1] # Height from bbox
|
| 19 |
+
|
| 20 |
+
# Calculate position to center the name
|
| 21 |
+
position = ((certificate.width - text_width) // 2, (certificate.height - text_height) // 2)
|
| 22 |
+
|
| 23 |
+
# Add the participant's name to the certificate in the center
|
| 24 |
+
draw.text(position, name, font=font, fill="black")
|
| 25 |
+
|
| 26 |
+
# Save the generated certificate with the name
|
| 27 |
+
certificate.save(f"{name}_certificate.png")
|
| 28 |
+
|
| 29 |
+
# Return the image to be displayed and downloaded in the Gradio interface
|
| 30 |
+
return certificate, f"{name}_certificate.png"
|
| 31 |
+
|
| 32 |
+
# Define the Gradio interface
|
| 33 |
+
iface = gr.Interface(
|
| 34 |
+
fn=generate_certificate,
|
| 35 |
+
inputs=gr.Textbox(label="Enter Your Name"),
|
| 36 |
+
outputs=[gr.Image(type="pil"), gr.File(label="Download Certificate")], # Added file output for download
|
| 37 |
+
title="Certificate Generator",
|
| 38 |
+
description="Enter your name below to generate your personalized certificate.",
|
| 39 |
+
live=True
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Launch the interface
|
| 43 |
+
iface.launch()
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|