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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -27
app.py CHANGED
@@ -26,23 +26,20 @@ def draw_lattice_grid(draw, width, height, spacing=100, color=(235, 235, 235)):
26
  draw.line([(0, y), (width, y)], fill=color, width=2)
27
 
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.
36
- style (str, optional): The background style ('plain', 'lines', 'dots', 'grid').
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
@@ -60,23 +57,19 @@ def text_to_images_generator(text_content, style='lines', font_path=None):
60
  # --- Font Loading ---
61
  font = None
62
  try:
63
- if font_path and os.path.exists(font_path):
64
- font = ImageFont.truetype(font_path, FONT_SIZE)
65
- else:
66
- font_paths_to_try = [
67
- "Arial.ttf", "arial.ttf", "DejaVuSans.ttf",
68
- "/System/Library/Fonts/Supplemental/Arial.ttf",
69
- "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
70
- "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"
71
- ]
72
- for f_path in font_paths_to_try:
73
- try:
74
- font = ImageFont.truetype(f_path, FONT_SIZE)
75
- break
76
- except IOError:
77
- continue
78
  if not font:
79
- gr.Warning("Could not find a standard .ttf font. Falling back to the basic default font.")
80
  font = ImageFont.load_default()
81
  except Exception as e:
82
  print(f"An unexpected error occurred during font loading: {e}")
@@ -172,7 +165,7 @@ def text_to_images_generator(text_content, style='lines', font_path=None):
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,9 +179,9 @@ demo = gr.Interface(
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
 
 
26
  draw.line([(0, y), (width, y)], fill=color, width=2)
27
 
28
 
29
+ def text_to_images_generator(text_content, style='lines'):
30
  """
31
+ Converts a given string of text into a single combined image and returns the file path.
32
  This is compatible with both UI and MCP.
33
 
34
  Args:
35
  text_content (str): The text to be converted.
36
+ style (str): The background style ('plain', 'lines', 'dots', 'grid').
 
37
 
38
  Returns:
39
+ str: Message with the path to the generated combined image file.
40
  """
41
  if not text_content or not text_content.strip():
42
+ return "Error: Input text is empty. Please enter some text to generate images."
 
 
43
 
44
  # --- Configuration ---
45
  IMG_WIDTH = 1080
 
57
  # --- Font Loading ---
58
  font = None
59
  try:
60
+ font_paths_to_try = [
61
+ "Arial.ttf", "arial.ttf", "DejaVuSans.ttf",
62
+ "/System/Library/Fonts/Supplemental/Arial.ttf",
63
+ "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
64
+ "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"
65
+ ]
66
+ for f_path in font_paths_to_try:
67
+ try:
68
+ font = ImageFont.truetype(f_path, FONT_SIZE)
69
+ break
70
+ except IOError:
71
+ continue
 
 
 
72
  if not font:
 
73
  font = ImageFont.load_default()
74
  except Exception as e:
75
  print(f"An unexpected error occurred during font loading: {e}")
 
165
  combined_img.save(temp_file.name, format='PNG')
166
  temp_file.close()
167
 
168
+ return f"Image successfully generated and saved to: {temp_file.name}\n\nImage details:\n- Total pages: {len(pages_content)}\n- Image dimensions: {IMG_WIDTH} x {total_height} pixels\n- Style: {style}\n- Format: PNG"
169
 
170
  # --- Gradio Interface ---
171
 
 
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.Textbox(label="Result", show_label=True),
183
  title="Text-to-Image Converter",
184
+ description="Transforms long-form text into a single combined image with multiple pages. Paste your text, choose a style, and click 'Submit'. The result will show the file path where your image was saved.",
185
  allow_flagging="never"
186
  )
187