Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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'
|
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
|
37 |
-
font_path (str, optional): The path to a .ttf font file.
|
38 |
|
39 |
Returns:
|
40 |
-
str:
|
41 |
"""
|
42 |
if not text_content or not text_content.strip():
|
43 |
-
|
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 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
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.
|
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'.
|
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 |
|