Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
-
import zipfile
|
4 |
import tempfile
|
5 |
from PIL import Image, ImageDraw, ImageFont
|
6 |
|
@@ -26,7 +25,7 @@ def draw_lattice_grid(draw, width, height, spacing=100, color=(235, 235, 235)):
|
|
26 |
|
27 |
def text_to_images_mcp(text_content, style='lines', font_path=None):
|
28 |
"""
|
29 |
-
Converts text to images and returns a
|
30 |
|
31 |
Args:
|
32 |
text_content (str): The text to be converted.
|
@@ -34,10 +33,10 @@ def text_to_images_mcp(text_content, style='lines', font_path=None):
|
|
34 |
font_path (str, optional): The path to a .ttf font file.
|
35 |
|
36 |
Returns:
|
37 |
-
|
38 |
"""
|
39 |
if not text_content.strip():
|
40 |
-
return
|
41 |
|
42 |
# --- Configuration ---
|
43 |
IMG_WIDTH = 1080
|
@@ -51,9 +50,6 @@ def text_to_images_mcp(text_content, style='lines', font_path=None):
|
|
51 |
FONT_SIZE = 48
|
52 |
LINE_SPACING = 20
|
53 |
|
54 |
-
# Create temporary directory for images
|
55 |
-
temp_dir = tempfile.mkdtemp()
|
56 |
-
|
57 |
# --- Font Loading ---
|
58 |
font = None
|
59 |
try:
|
@@ -81,7 +77,7 @@ def text_to_images_mcp(text_content, style='lines', font_path=None):
|
|
81 |
paragraphs = [p.strip() for p in text_content.strip().split('\n') if p.strip()]
|
82 |
|
83 |
if not paragraphs:
|
84 |
-
return
|
85 |
|
86 |
all_lines_and_breaks = []
|
87 |
for i, paragraph in enumerate(paragraphs):
|
@@ -117,10 +113,10 @@ def text_to_images_mcp(text_content, style='lines', font_path=None):
|
|
117 |
line_height = 12
|
118 |
|
119 |
PARAGRAPH_SPACING = line_height
|
120 |
-
|
121 |
|
122 |
def create_image_page(content, page_num):
|
123 |
-
"""Helper function to create and
|
124 |
img = Image.new('RGB', (IMG_WIDTH, IMG_HEIGHT), color=BACKGROUND_COLOR)
|
125 |
draw = ImageDraw.Draw(img)
|
126 |
|
@@ -142,10 +138,8 @@ def text_to_images_mcp(text_content, style='lines', font_path=None):
|
|
142 |
else:
|
143 |
current_y += PARAGRAPH_SPACING
|
144 |
|
145 |
-
|
146 |
-
img
|
147 |
-
image_paths.append(filename)
|
148 |
-
return filename
|
149 |
|
150 |
for item in all_lines_and_breaks:
|
151 |
is_break = item is None
|
@@ -165,13 +159,7 @@ def text_to_images_mcp(text_content, style='lines', font_path=None):
|
|
165 |
img_count += 1
|
166 |
create_image_page(page_content, img_count)
|
167 |
|
168 |
-
|
169 |
-
zip_path = os.path.join(tempfile.gettempdir(), f"text_to_images_{len(os.listdir(temp_dir))}_pages.zip")
|
170 |
-
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
171 |
-
for img_path in image_paths:
|
172 |
-
zipf.write(img_path, os.path.basename(img_path))
|
173 |
-
|
174 |
-
return zip_path
|
175 |
|
176 |
# Sample text for demonstration
|
177 |
sample_text = """In the heart of a bustling city, there lived a clockmaker named Alistair. His shop, a quaint corner of tranquility amidst the urban chaos, was filled with the gentle ticking of countless timepieces. Each clock was a masterpiece, a testament to his dedication and skill. But Alistair held a secret. One of his clocks, an old grandfather clock in the corner, did not just tell time. It told stories.
|
@@ -204,13 +192,19 @@ demo = gr.Interface(
|
|
204 |
)
|
205 |
],
|
206 |
outputs=[
|
207 |
-
gr.
|
208 |
-
label="
|
209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
)
|
211 |
],
|
212 |
title="π Text to Images Converter",
|
213 |
-
description="Transform long-form text into a series of attractive, readable images. Simply paste your text, choose a background style, and
|
214 |
examples=[
|
215 |
[sample_text, 'lines', ''],
|
216 |
[sample_text, 'dots', ''],
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
|
|
3 |
import tempfile
|
4 |
from PIL import Image, ImageDraw, ImageFont
|
5 |
|
|
|
25 |
|
26 |
def text_to_images_mcp(text_content, style='lines', font_path=None):
|
27 |
"""
|
28 |
+
Converts text to images and returns a list of image objects.
|
29 |
|
30 |
Args:
|
31 |
text_content (str): The text to be converted.
|
|
|
33 |
font_path (str, optional): The path to a .ttf font file.
|
34 |
|
35 |
Returns:
|
36 |
+
list: List of PIL Image objects for display in Gradio.
|
37 |
"""
|
38 |
if not text_content.strip():
|
39 |
+
return []
|
40 |
|
41 |
# --- Configuration ---
|
42 |
IMG_WIDTH = 1080
|
|
|
50 |
FONT_SIZE = 48
|
51 |
LINE_SPACING = 20
|
52 |
|
|
|
|
|
|
|
53 |
# --- Font Loading ---
|
54 |
font = None
|
55 |
try:
|
|
|
77 |
paragraphs = [p.strip() for p in text_content.strip().split('\n') if p.strip()]
|
78 |
|
79 |
if not paragraphs:
|
80 |
+
return []
|
81 |
|
82 |
all_lines_and_breaks = []
|
83 |
for i, paragraph in enumerate(paragraphs):
|
|
|
113 |
line_height = 12
|
114 |
|
115 |
PARAGRAPH_SPACING = line_height
|
116 |
+
generated_images = []
|
117 |
|
118 |
def create_image_page(content, page_num):
|
119 |
+
"""Helper function to create and return a single image page."""
|
120 |
img = Image.new('RGB', (IMG_WIDTH, IMG_HEIGHT), color=BACKGROUND_COLOR)
|
121 |
draw = ImageDraw.Draw(img)
|
122 |
|
|
|
138 |
else:
|
139 |
current_y += PARAGRAPH_SPACING
|
140 |
|
141 |
+
generated_images.append(img)
|
142 |
+
return img
|
|
|
|
|
143 |
|
144 |
for item in all_lines_and_breaks:
|
145 |
is_break = item is None
|
|
|
159 |
img_count += 1
|
160 |
create_image_page(page_content, img_count)
|
161 |
|
162 |
+
return generated_images
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
# Sample text for demonstration
|
165 |
sample_text = """In the heart of a bustling city, there lived a clockmaker named Alistair. His shop, a quaint corner of tranquility amidst the urban chaos, was filled with the gentle ticking of countless timepieces. Each clock was a masterpiece, a testament to his dedication and skill. But Alistair held a secret. One of his clocks, an old grandfather clock in the corner, did not just tell time. It told stories.
|
|
|
192 |
)
|
193 |
],
|
194 |
outputs=[
|
195 |
+
gr.Gallery(
|
196 |
+
label="Generated Images",
|
197 |
+
show_label=True,
|
198 |
+
elem_id="gallery",
|
199 |
+
columns=2,
|
200 |
+
rows=2,
|
201 |
+
object_fit="contain",
|
202 |
+
height="auto",
|
203 |
+
show_download_button=True
|
204 |
)
|
205 |
],
|
206 |
title="π Text to Images Converter",
|
207 |
+
description="Transform long-form text into a series of attractive, readable images. Simply paste your text, choose a background style, and preview the generated images. You can download individual images by clicking on them.",
|
208 |
examples=[
|
209 |
[sample_text, 'lines', ''],
|
210 |
[sample_text, 'dots', ''],
|