Nojoodalnahdi commited on
Commit
dd25414
1 Parent(s): c941612

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -28
app.py CHANGED
@@ -2,35 +2,29 @@ import gradio as gr
2
  from PIL import Image, ImageDraw, ImageFont
3
 
4
  def greet(name):
5
- try:
6
- # Create an image with white background
7
- img = Image.new('RGB', (500, 300), color=(255, 255, 255))
8
-
9
- # Initialize the drawing context with the image object
10
- d = ImageDraw.Draw(img)
11
-
12
- # Use the default font provided by PIL
13
- font = ImageFont.load_default()
14
-
15
- # Define the text
16
- text = f"Hello {name}!!"
17
-
18
- # Get the size of the text to center it
19
- text_width, text_height = d.textsize(text, font=font)
20
-
21
- # Calculate the x, y position of the text
22
- x = (img.width - text_width) // 2
23
- y = (img.height - text_height) // 2
24
-
25
- # Add text to the image
26
- d.text((x, y), text, fill=(0, 0, 0), font=font)
27
-
28
- # Return the image
29
- return img
30
 
31
- except Exception as e:
32
- print(f"Error generating image: {e}")
33
- return None # Return None if there's an error to avoid breaking the app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  # Set up the Gradio interface
36
  demo = gr.Interface(fn=greet, inputs="text", outputs="image")
 
2
  from PIL import Image, ImageDraw, ImageFont
3
 
4
  def greet(name):
5
+ # Create an image with a blue background
6
+ img = Image.new('RGB', (500, 300), color=(100, 150, 255))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ # Initialize the drawing context with the image object
9
+ draw = ImageDraw.Draw(img)
10
+
11
+ # Use the default font provided by PIL
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")