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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -37
app.py CHANGED
@@ -28,8 +28,8 @@ def draw_lattice_grid(draw, width, height, spacing=100, color=(235, 235, 235)):
28
 
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,12 +37,12 @@ def text_to_images_generator(text_content, style='lines', font_path=None):
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
44
  gr.Warning("Input text is empty. Please enter some text to generate images.")
45
- return []
46
 
47
  # --- Configuration ---
48
  IMG_WIDTH = 1080
@@ -112,10 +112,6 @@ def text_to_images_generator(text_content, style='lines', font_path=None):
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
-
119
  try:
120
  line_height = font.getbbox("A")[3] - font.getbbox("A")[1]
121
  except AttributeError:
@@ -123,10 +119,34 @@ def text_to_images_generator(text_content, style='lines', font_path=None):
123
 
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
 
131
  if style == 'lines':
132
  line_style_spacing = line_height + LINE_SPACING
@@ -137,35 +157,22 @@ def text_to_images_generator(text_content, style='lines', font_path=None):
137
  draw_lattice_grid(draw, IMG_WIDTH, IMG_HEIGHT, color=STYLE_COLOR)
138
 
139
  current_y = PADDING_Y
140
- for page_item in content:
141
  if page_item is not None:
142
  draw.text((PADDING_X, current_y), page_item, font=font, fill=TEXT_COLOR)
143
  current_y += line_height + LINE_SPACING
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
155
- item_height = PARAGRAPH_SPACING if is_break else line_height
156
-
157
- if y_text + item_height > IMG_HEIGHT - PADDING_Y:
158
- create_image_page(page_content)
159
- page_content = [item]
160
- y_text = PADDING_Y + item_height + (0 if is_break else LINE_SPACING)
161
- else:
162
- page_content.append(item)
163
- y_text += item_height + (0 if is_break else LINE_SPACING)
164
-
165
- if page_content:
166
- create_image_page(page_content)
167
-
168
- return generated_image_paths
169
 
170
  # --- Gradio Interface ---
171
 
@@ -179,9 +186,9 @@ demo = gr.Interface(
179
  gr.Textbox(lines=15, label="Text Content", placeholder="Paste your long-form text here...", value=example_text),
180
  gr.Radio(['lines', 'dots', 'grid', 'plain'], label="Background Style", value='lines')
181
  ],
182
- outputs=gr.Gallery(label="Generated Images", show_label=True, preview=True),
183
  title="Text-to-Image Converter",
184
- description="Transforms long-form text into a series of attractive, readable images. Paste your text, choose a style, and click 'Submit'. You can download the images from the gallery below.",
185
  allow_flagging="never"
186
  )
187
 
 
28
 
29
  def text_to_images_generator(text_content, style='lines', font_path=None):
30
  """
31
+ Converts a given string of text into a single combined image.
32
+ 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
+ str: Path to the generated combined image file.
41
  """
42
  if not text_content or not text_content.strip():
43
+ # Return None and show a warning if there is no text
44
  gr.Warning("Input text is empty. Please enter some text to generate images.")
45
+ return None
46
 
47
  # --- Configuration ---
48
  IMG_WIDTH = 1080
 
112
  all_lines_and_breaks.append(None)
113
 
114
  # --- Image Generation ---
 
 
 
 
115
  try:
116
  line_height = font.getbbox("A")[3] - font.getbbox("A")[1]
117
  except AttributeError:
 
119
 
120
  PARAGRAPH_SPACING = line_height
121
 
122
+ # Calculate pages and total height needed
123
+ pages_content = []
124
+ current_page = []
125
+ y_text = PADDING_Y
126
+
127
+ for item in all_lines_and_breaks:
128
+ is_break = item is None
129
+ item_height = PARAGRAPH_SPACING if is_break else line_height
130
+
131
+ if y_text + item_height > IMG_HEIGHT - PADDING_Y:
132
+ pages_content.append(current_page)
133
+ current_page = [item]
134
+ y_text = PADDING_Y + item_height + (0 if is_break else LINE_SPACING)
135
+ else:
136
+ current_page.append(item)
137
+ y_text += item_height + (0 if is_break else LINE_SPACING)
138
+
139
+ if current_page:
140
+ pages_content.append(current_page)
141
+
142
+ # Create a single combined image with all pages
143
+ total_height = len(pages_content) * IMG_HEIGHT
144
+ combined_img = Image.new('RGB', (IMG_WIDTH, total_height), color=BACKGROUND_COLOR)
145
+
146
+ for page_idx, page_content in enumerate(pages_content):
147
+ # Create individual page
148
+ page_img = Image.new('RGB', (IMG_WIDTH, IMG_HEIGHT), color=BACKGROUND_COLOR)
149
+ draw = ImageDraw.Draw(page_img)
150
 
151
  if style == 'lines':
152
  line_style_spacing = line_height + LINE_SPACING
 
157
  draw_lattice_grid(draw, IMG_WIDTH, IMG_HEIGHT, color=STYLE_COLOR)
158
 
159
  current_y = PADDING_Y
160
+ for page_item in page_content:
161
  if page_item is not None:
162
  draw.text((PADDING_X, current_y), page_item, font=font, fill=TEXT_COLOR)
163
  current_y += line_height + LINE_SPACING
164
  else:
165
  current_y += PARAGRAPH_SPACING
166
 
167
+ # Paste this page onto the combined image
168
+ combined_img.paste(page_img, (0, page_idx * IMG_HEIGHT))
 
 
 
169
 
170
+ # Save combined image to temporary file
171
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
172
+ combined_img.save(temp_file.name, format='PNG')
173
+ temp_file.close()
174
+
175
+ return temp_file.name
 
 
 
 
 
 
 
 
 
 
176
 
177
  # --- Gradio Interface ---
178
 
 
186
  gr.Textbox(lines=15, label="Text Content", placeholder="Paste your long-form text here...", value=example_text),
187
  gr.Radio(['lines', 'dots', 'grid', 'plain'], label="Background Style", value='lines')
188
  ],
189
+ outputs=gr.Image(label="Generated Image", show_label=True),
190
  title="Text-to-Image Converter",
191
+ description="Transforms long-form text into a single combined image with multiple pages. Paste your text, choose a style, and click 'Submit'. You can download the image from below.",
192
  allow_flagging="never"
193
  )
194