dschandra commited on
Commit
cac4307
·
verified ·
1 Parent(s): b681210

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -13
app.py CHANGED
@@ -1,28 +1,36 @@
1
  import gradio as gr
2
  from PIL import Image, ImageDraw, ImageFont
3
  import random
 
4
 
5
- # Placeholder function for image generation (replace with actual model inference if desired)
6
  def generate_image(text_description):
7
- # Create a blank image
8
  img = Image.new('RGB', (512, 512), color=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
9
  draw = ImageDraw.Draw(img)
10
 
11
- # Add text to the image (simulating a generated image based on description)
12
  try:
13
- font = ImageFont.truetype("arial.ttf", 40)
14
  except:
15
  font = ImageFont.load_default()
16
 
17
- # Wrap text description if too long
18
- text = text_description if len(text_description) < 30 else text_description[:27] + "..."
19
- draw.text((20, 20), f"Generated: {text}", font=font, fill=(255, 255, 255))
 
20
 
21
- # Here you could integrate a real model like Stable Diffusion
22
- # Example (commented out):
23
- # from diffusers import StableDiffusionPipeline
24
- # pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
25
- # img = pipe(text_description).images[0]
 
 
 
 
 
 
26
 
27
  return img
28
 
@@ -33,7 +41,7 @@ with gr.Blocks(title="Text-to-Image Generator") as demo:
33
 
34
  with gr.Row():
35
  with gr.Column():
36
- text_input = gr.Textbox(label="Description", placeholder="Type your image description here...")
37
  generate_btn = gr.Button("Generate Image")
38
  with gr.Column():
39
  output_image = gr.Image(label="Generated Image")
 
1
  import gradio as gr
2
  from PIL import Image, ImageDraw, ImageFont
3
  import random
4
+ import textwrap
5
 
6
+ # Improved function for image generation with better text handling
7
  def generate_image(text_description):
8
+ # Create a blank image with a random background color
9
  img = Image.new('RGB', (512, 512), color=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
10
  draw = ImageDraw.Draw(img)
11
 
12
+ # Load a default font (fallback to system font if arial.ttf is unavailable)
13
  try:
14
+ font = ImageFont.truetype("arial.ttf", 30)
15
  except:
16
  font = ImageFont.load_default()
17
 
18
+ # Wrap text to fit within the image
19
+ wrapper = textwrap.TextWrapper(width=20) # Adjust width for better wrapping
20
+ wrapped_text = wrapper.fill(text=text_description)
21
+ lines = wrapped_text.split('\n')
22
 
23
+ # Draw each line of text
24
+ y_text = 20
25
+ for line in lines:
26
+ line_width, line_height = draw.textsize(line, font=font)
27
+ draw.text((20, y_text), line, font=font, fill=(255, 255, 255))
28
+ y_text += line_height + 5 # Add spacing between lines
29
+
30
+ # Optional: Add a simple graphical element to represent the description
31
+ if "lake" in text_description.lower() or "mountains" in text_description.lower():
32
+ draw.rectangle([200, 300, 300, 400], fill=(0, 191, 255)) # Blue rectangle for lake
33
+ draw.polygon([(250, 250), (300, 200), (350, 250)], fill=(255, 255, 255)) # White triangle for mountain
34
 
35
  return img
36
 
 
41
 
42
  with gr.Row():
43
  with gr.Column():
44
+ text_input = gr.Textbox(label="Description", placeholder="Type your image description here...", value="A serene lake surrounded by snow-capped mountains under a vibrant sunset sky with shades of orange and purple.")
45
  generate_btn = gr.Button("Generate Image")
46
  with gr.Column():
47
  output_image = gr.Image(label="Generated Image")