VirtualOasis commited on
Commit
4932818
·
verified ·
1 Parent(s): 52a9d37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -6
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  import gradio as gr
3
  from PIL import Image, ImageDraw, ImageFont
4
 
@@ -28,7 +29,7 @@ def draw_lattice_grid(draw, width, height, spacing=100, color=(235, 235, 235)):
28
  def text_to_images_generator(text_content, style='lines', font_path=None):
29
  """
30
  Converts a given string of text into a series of images and returns them
31
- as a list of PIL Image objects. This is compatible with both UI and MCP.
32
 
33
  Args:
34
  text_content (str): The text to be converted.
@@ -36,7 +37,7 @@ def text_to_images_generator(text_content, style='lines', font_path=None):
36
  font_path (str, optional): The path to a .ttf font file.
37
 
38
  Returns:
39
- list: A list of PIL.Image.Image objects.
40
  """
41
  if not text_content or not text_content.strip():
42
  # Return empty list and show a warning if there is no text
@@ -111,7 +112,7 @@ def text_to_images_generator(text_content, style='lines', font_path=None):
111
  all_lines_and_breaks.append(None)
112
 
113
  # --- Image Generation ---
114
- generated_images = [] # Store PIL Image objects directly
115
  page_content = []
116
  y_text = PADDING_Y
117
 
@@ -123,7 +124,7 @@ def text_to_images_generator(text_content, style='lines', font_path=None):
123
  PARAGRAPH_SPACING = line_height
124
 
125
  def create_image_page(content):
126
- """Helper function to create a single image page and return it."""
127
  img = Image.new('RGB', (IMG_WIDTH, IMG_HEIGHT), color=BACKGROUND_COLOR)
128
  draw = ImageDraw.Draw(img)
129
 
@@ -143,7 +144,11 @@ def text_to_images_generator(text_content, style='lines', font_path=None):
143
  else:
144
  current_y += PARAGRAPH_SPACING
145
 
146
- generated_images.append(img) # Add the PIL image to our list
 
 
 
 
147
 
148
  for item in all_lines_and_breaks:
149
  is_break = item is None
@@ -160,7 +165,7 @@ def text_to_images_generator(text_content, style='lines', font_path=None):
160
  if page_content:
161
  create_image_page(page_content)
162
 
163
- return generated_images
164
 
165
  # --- Gradio Interface ---
166
 
 
1
  import os
2
+ import tempfile
3
  import gradio as gr
4
  from PIL import Image, ImageDraw, ImageFont
5
 
 
29
  def text_to_images_generator(text_content, style='lines', font_path=None):
30
  """
31
  Converts a given string of text into a series of images and returns them
32
+ as a list of image file paths. This is compatible with both UI and MCP.
33
 
34
  Args:
35
  text_content (str): The text to be converted.
 
37
  font_path (str, optional): The path to a .ttf font file.
38
 
39
  Returns:
40
+ list: A list of image file paths.
41
  """
42
  if not text_content or not text_content.strip():
43
  # Return empty list and show a warning if there is no text
 
112
  all_lines_and_breaks.append(None)
113
 
114
  # --- Image Generation ---
115
+ generated_image_paths = [] # Store image file paths
116
  page_content = []
117
  y_text = PADDING_Y
118
 
 
124
  PARAGRAPH_SPACING = line_height
125
 
126
  def create_image_page(content):
127
+ """Helper function to create a single image page and save it to a temporary file."""
128
  img = Image.new('RGB', (IMG_WIDTH, IMG_HEIGHT), color=BACKGROUND_COLOR)
129
  draw = ImageDraw.Draw(img)
130
 
 
144
  else:
145
  current_y += PARAGRAPH_SPACING
146
 
147
+ # Save image to temporary file
148
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
149
+ img.save(temp_file.name, format='PNG')
150
+ temp_file.close()
151
+ generated_image_paths.append(temp_file.name)
152
 
153
  for item in all_lines_and_breaks:
154
  is_break = item is None
 
165
  if page_content:
166
  create_image_page(page_content)
167
 
168
+ return generated_image_paths
169
 
170
  # --- Gradio Interface ---
171