Sugamdeol commited on
Commit
7d205dd
·
verified ·
1 Parent(s): 0063f85

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -11
app.py CHANGED
@@ -3,10 +3,8 @@ import requests
3
  from PIL import Image, ImageDraw, ImageFont
4
  import textwrap
5
  import os
6
- import time
7
  from concurrent.futures import ThreadPoolExecutor, as_completed
8
 
9
- # Function to download image based on prompt
10
  def download_image(prompt, index):
11
  try:
12
  url = f"https://pollinations.ai/p/{prompt}"
@@ -20,26 +18,51 @@ def download_image(prompt, index):
20
  print(f"Error downloading image {index}: {e}")
21
  return None
22
 
23
- # Function to add text to image
24
  def add_text_to_image(image_path, text):
25
  try:
26
  img = Image.open(image_path)
27
  draw = ImageDraw.Draw(img)
28
- # Use a default font if arial.ttf is not available
 
29
  try:
30
- font = ImageFont.truetype("arial.ttf", 20)
31
  except IOError:
32
- font = ImageFont.load_default()
33
- lines = textwrap.fill(text, width=40)
34
- draw.text((10, 10), lines, font=font, fill="white", stroke_width=2, stroke_fill="black")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  img_with_text = f'text_added_{os.path.basename(image_path)}'
 
36
  img.save(img_with_text)
37
  return img_with_text
38
  except Exception as e:
39
  print(f"Error adding text to image: {e}")
40
  return None
41
 
42
- # Function to visualize each line of the story
43
  def visualize_story_lines(story, progress=gr.Progress()):
44
  lines = [line.strip() for line in story.split('\n') if line.strip()]
45
  images_with_text = []
@@ -61,13 +84,11 @@ def visualize_story_lines(story, progress=gr.Progress()):
61
 
62
  return [img for _, img in sorted(images_with_text)]
63
 
64
- # Gradio interface with state management
65
  def visualize_story_images(story):
66
  if not story.strip():
67
  return []
68
  return visualize_story_lines(story)
69
 
70
- # Create a Gradio interface
71
  with gr.Blocks() as iface:
72
  gr.Markdown("# Story Visualizer")
73
 
 
3
  from PIL import Image, ImageDraw, ImageFont
4
  import textwrap
5
  import os
 
6
  from concurrent.futures import ThreadPoolExecutor, as_completed
7
 
 
8
  def download_image(prompt, index):
9
  try:
10
  url = f"https://pollinations.ai/p/{prompt}"
 
18
  print(f"Error downloading image {index}: {e}")
19
  return None
20
 
 
21
  def add_text_to_image(image_path, text):
22
  try:
23
  img = Image.open(image_path)
24
  draw = ImageDraw.Draw(img)
25
+
26
+ # Use a default font if custom font is not available
27
  try:
28
+ font = ImageFont.truetype("arial.ttf", 30)
29
  except IOError:
30
+ font = ImageFont.load_default().font_variant(size=30)
31
+
32
+ # Calculate text size and position
33
+ img_width, img_height = img.size
34
+ max_text_width = int(img_width * 0.9) # 90% of image width
35
+ lines = textwrap.wrap(text, width=40)
36
+ line_height = font.getsize('hg')[1] + 5 # Add some padding
37
+ text_height = line_height * len(lines)
38
+
39
+ # Create semi-transparent background
40
+ overlay = Image.new('RGBA', img.size, (0, 0, 0, 0))
41
+ overlay_draw = ImageDraw.Draw(overlay)
42
+ overlay_draw.rectangle([(0, img_height - text_height - 20), (img_width, img_height)],
43
+ fill=(0, 0, 0, 180))
44
+
45
+ # Paste the overlay onto the original image
46
+ img = img.convert('RGBA')
47
+ img = Image.alpha_composite(img, overlay)
48
+ draw = ImageDraw.Draw(img)
49
+
50
+ # Add text
51
+ y_text = img_height - text_height - 10
52
+ for line in lines:
53
+ line_width, _ = draw.textsize(line, font=font)
54
+ x_text = (img_width - line_width) / 2
55
+ draw.text((x_text, y_text), line, font=font, fill=(255, 255, 255, 255))
56
+ y_text += line_height
57
+
58
  img_with_text = f'text_added_{os.path.basename(image_path)}'
59
+ img = img.convert('RGB')
60
  img.save(img_with_text)
61
  return img_with_text
62
  except Exception as e:
63
  print(f"Error adding text to image: {e}")
64
  return None
65
 
 
66
  def visualize_story_lines(story, progress=gr.Progress()):
67
  lines = [line.strip() for line in story.split('\n') if line.strip()]
68
  images_with_text = []
 
84
 
85
  return [img for _, img in sorted(images_with_text)]
86
 
 
87
  def visualize_story_images(story):
88
  if not story.strip():
89
  return []
90
  return visualize_story_lines(story)
91
 
 
92
  with gr.Blocks() as iface:
93
  gr.Markdown("# Story Visualizer")
94