Nojoodalnahdi
commited on
Commit
•
dc7375b
1
Parent(s):
dd25414
Update app.py
Browse files
app.py
CHANGED
@@ -2,29 +2,43 @@ import gradio as gr
|
|
2 |
from PIL import Image, ImageDraw, ImageFont
|
3 |
|
4 |
def greet(name):
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
font = ImageFont.load_default()
|
13 |
-
|
14 |
-
# Define the text to be added to the image
|
15 |
-
text = f"Hello {name}!!"
|
16 |
-
|
17 |
-
# Calculate the size of the text to center it
|
18 |
-
text_width, text_height = draw.textsize(text, font=font)
|
19 |
-
|
20 |
-
# Calculate the x, y position of the text to be centered
|
21 |
-
x = (img.width - text_width) // 2
|
22 |
-
y = (img.height - text_height) // 2
|
23 |
-
|
24 |
-
# Draw the text on the image
|
25 |
-
draw.text((x, y), text, fill=(255, 255, 255), font=font) # White text color
|
26 |
-
|
27 |
-
return img
|
28 |
|
29 |
# Set up the Gradio interface
|
30 |
demo = gr.Interface(fn=greet, inputs="text", outputs="image")
|
|
|
2 |
from PIL import Image, ImageDraw, ImageFont
|
3 |
|
4 |
def greet(name):
|
5 |
+
try:
|
6 |
+
# Create an image with a blue background
|
7 |
+
img = Image.new('RGB', (500, 300), color=(100, 150, 255))
|
8 |
+
|
9 |
+
# Initialize the drawing context with the image object
|
10 |
+
draw = ImageDraw.Draw(img)
|
11 |
+
|
12 |
+
# Path to the custom font file
|
13 |
+
font_path = "fonts/arial.ttf" # Adjust the path as needed
|
14 |
+
|
15 |
+
try:
|
16 |
+
# Try to use the custom font
|
17 |
+
font = ImageFont.truetype(font_path, 40)
|
18 |
+
except IOError:
|
19 |
+
# Fallback to default font if the custom font is not found
|
20 |
+
print("Custom font not found, using default font.")
|
21 |
+
font = ImageFont.load_default()
|
22 |
+
|
23 |
+
# Define the text to be added to the image
|
24 |
+
text = f"Hello {name}!!"
|
25 |
+
|
26 |
+
# Calculate the size of the text to center it
|
27 |
+
text_width, text_height = draw.textsize(text, font=font)
|
28 |
+
|
29 |
+
# Calculate the x, y position of the text to be centered
|
30 |
+
x = (img.width - text_width) // 2
|
31 |
+
y = (img.height - text_height) // 2
|
32 |
+
|
33 |
+
# Draw the text on the image
|
34 |
+
draw.text((x, y), text, fill=(255, 255, 255), font=font) # White text color
|
35 |
+
|
36 |
+
return img
|
37 |
|
38 |
+
except Exception as e:
|
39 |
+
# Print the error message for debugging
|
40 |
+
print(f"Error generating image: {e}")
|
41 |
+
return None # Return None if there's an error to avoid breaking the app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
# Set up the Gradio interface
|
44 |
demo = gr.Interface(fn=greet, inputs="text", outputs="image")
|