Spaces:
Runtime error
Runtime error
import gradio as gr | |
from PIL import Image, ImageDraw, ImageFont | |
def greet(name): | |
# Create an image with white background | |
img = Image.new('RGB', (500, 300), color=(255, 255, 255)) | |
# Initialize the drawing context with the image object | |
d = ImageDraw.Draw(img) | |
# Use the default font provided by PIL (no need to specify a path) | |
font = ImageFont.load_default() | |
# Define the text | |
text = f"Hello {name}!!" | |
# Get the size of the text to center it | |
text_width, text_height = d.textsize(text, font=font) | |
# Calculate the x, y position of the text | |
x = (img.width - text_width) // 2 | |
y = (img.height - text_height) // 2 | |
# Add text to the image | |
d.text((x, y), text, fill=(0, 0, 0), font=font) | |
# Return the image | |
return img | |
# Set up the Gradio interface | |
demo = gr.Interface(fn=greet, inputs="text", outputs="image") | |
# Launch the app | |
demo.launch() | |