Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -29,28 +29,16 @@ def text_to_images_and_base64_generator(text_content, style, font_size, bg_color
|
|
29 |
"""
|
30 |
Converts text into images, returning both PIL objects for UI preview
|
31 |
and base64 strings for an API/MCP client, with customizable options.
|
32 |
-
|
33 |
-
Args:
|
34 |
-
text_content (str): The text to be converted.
|
35 |
-
style (str): The background style ('plain', 'lines', 'dots', 'grid').
|
36 |
-
font_size (int): The font size for the text.
|
37 |
-
bg_color_name (str): The name of the background color.
|
38 |
-
aspect_ratio_str (str): The string representing the desired aspect ratio.
|
39 |
-
font_choice (str): The name of the font family to use.
|
40 |
-
|
41 |
-
Returns:
|
42 |
-
tuple: A tuple containing (list of PIL.Image.Image, list of base64 strings).
|
43 |
"""
|
44 |
if not text_content or not text_content.strip():
|
45 |
gr.Warning("Input text is empty. Please enter some text to generate images.")
|
46 |
return [], []
|
47 |
|
48 |
-
# ---
|
49 |
aspect_ratio_map = {
|
50 |
"1:1 (Square)": (1080, 1080),
|
51 |
"16:9 (Widescreen)": (1080, 608),
|
52 |
"9:16 (Vertical)": (1080, 1920),
|
53 |
-
"3:4 (Standard)": (1080, 1440),
|
54 |
"4:3 (Standard)": (1080, 810)
|
55 |
}
|
56 |
color_map = {
|
@@ -59,14 +47,15 @@ def text_to_images_and_base64_generator(text_content, style, font_size, bg_color
|
|
59 |
"Beige": (245, 245, 220),
|
60 |
"Pale Blue": (220, 230, 245)
|
61 |
}
|
|
|
|
|
62 |
font_map = {
|
63 |
-
"Arial":
|
64 |
-
"Times New Roman":
|
65 |
-
"Courier New":
|
66 |
-
"
|
67 |
-
"Georgia": ["georgia.ttf", "Georgia.ttf"],
|
68 |
-
"DejaVu Sans": ["DejaVuSans.ttf"] # Good fallback
|
69 |
}
|
|
|
70 |
IMG_WIDTH, IMG_HEIGHT = aspect_ratio_map.get(aspect_ratio_str, (1080, 1080))
|
71 |
BACKGROUND_COLOR = color_map.get(bg_color_name, (255, 255, 255))
|
72 |
|
@@ -79,25 +68,29 @@ def text_to_images_and_base64_generator(text_content, style, font_size, bg_color
|
|
79 |
|
80 |
# --- Font Loading ---
|
81 |
font = None
|
82 |
-
|
83 |
-
|
84 |
-
font_paths_to_try = selected_font_files + [
|
85 |
-
"Arial.ttf", "arial.ttf", "DejaVuSans.ttf",
|
86 |
-
"/System/Library/Fonts/Supplemental/Arial.ttf",
|
87 |
-
"/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
|
88 |
-
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"
|
89 |
-
]
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
if not font:
|
99 |
font = ImageFont.load_default()
|
100 |
-
gr.Warning(f"Could not
|
|
|
101 |
|
102 |
# --- Text Wrapping ---
|
103 |
drawable_width = IMG_WIDTH - 2 * PADDING_X
|
@@ -194,6 +187,9 @@ example_text = """In the heart of a bustling city, there lived a clockmaker name
|
|
194 |
|
195 |
One day, a young girl with eyes as curious as a cat's wandered into his shop. She wasn't interested in the shiny new watches but was drawn to the grandfather clock in the corner. "What's its story?" she asked, her voice soft. Alistair smiled, for he knew he had found the next guardian of the stories. The legacy of the whispering clock would live on."""
|
196 |
|
|
|
|
|
|
|
197 |
demo = gr.Interface(
|
198 |
fn=text_to_images_and_base64_generator,
|
199 |
inputs=[
|
@@ -202,7 +198,7 @@ demo = gr.Interface(
|
|
202 |
gr.Slider(24, 72, value=48, step=2, label="Font Size"),
|
203 |
gr.Radio(["White", "Light Gray", "Beige", "Pale Blue"], label="Background Color", value="White"),
|
204 |
gr.Radio(["1:1 (Square)", "16:9 (Widescreen)", "9:16 (Vertical)", "4:3 (Standard)"], label="Aspect Ratio", value="1:1 (Square)"),
|
205 |
-
gr.Dropdown(
|
206 |
],
|
207 |
outputs=[
|
208 |
gr.Gallery(label="Generated Images (Preview)", show_label=True, preview=True),
|
@@ -215,4 +211,11 @@ demo = gr.Interface(
|
|
215 |
|
216 |
# --- Main Execution ---
|
217 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
demo.launch(mcp_server=True)
|
|
|
29 |
"""
|
30 |
Converts text into images, returning both PIL objects for UI preview
|
31 |
and base64 strings for an API/MCP client, with customizable options.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
"""
|
33 |
if not text_content or not text_content.strip():
|
34 |
gr.Warning("Input text is empty. Please enter some text to generate images.")
|
35 |
return [], []
|
36 |
|
37 |
+
# --- Mappings for UI options ---
|
38 |
aspect_ratio_map = {
|
39 |
"1:1 (Square)": (1080, 1080),
|
40 |
"16:9 (Widescreen)": (1080, 608),
|
41 |
"9:16 (Vertical)": (1080, 1920),
|
|
|
42 |
"4:3 (Standard)": (1080, 810)
|
43 |
}
|
44 |
color_map = {
|
|
|
47 |
"Beige": (245, 245, 220),
|
48 |
"Pale Blue": (220, 230, 245)
|
49 |
}
|
50 |
+
# **IMPORTANT**: This now points to specific files you need to upload.
|
51 |
+
# The key is the user-friendly name, the value is the file path.
|
52 |
font_map = {
|
53 |
+
"Arial": os.path.join("fonts", "LiberationSans-Regular.ttf"),
|
54 |
+
"Times New Roman": os.path.join("fonts", "LiberationSerif-Regular.ttf"),
|
55 |
+
"Courier New": os.path.join("fonts", "DejaVuSansMono-Bold.ttf"),
|
56 |
+
"DejaVu Sans": os.path.join("fonts", "DejaVuSans.ttf")
|
|
|
|
|
57 |
}
|
58 |
+
|
59 |
IMG_WIDTH, IMG_HEIGHT = aspect_ratio_map.get(aspect_ratio_str, (1080, 1080))
|
60 |
BACKGROUND_COLOR = color_map.get(bg_color_name, (255, 255, 255))
|
61 |
|
|
|
68 |
|
69 |
# --- Font Loading ---
|
70 |
font = None
|
71 |
+
font_path = font_map.get(font_choice, os.path.join("fonts", "DejaVuSans.ttf"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
+
try:
|
74 |
+
# Check if the fonts directory and the specific font file exist
|
75 |
+
if os.path.exists(font_path):
|
76 |
+
font = ImageFont.truetype(font_path, font_size)
|
77 |
+
else:
|
78 |
+
# Fallback if font is not found in the local directory
|
79 |
+
gr.Warning(f"Font file not found at '{font_path}'. Trying system fallbacks.")
|
80 |
+
font_paths_to_try = ["/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"]
|
81 |
+
for f_path in font_paths_to_try:
|
82 |
+
try:
|
83 |
+
font = ImageFont.truetype(f_path, font_size)
|
84 |
+
break
|
85 |
+
except IOError:
|
86 |
+
continue
|
87 |
+
except IOError:
|
88 |
+
pass # Will be handled by the final fallback
|
89 |
|
90 |
if not font:
|
91 |
font = ImageFont.load_default()
|
92 |
+
gr.Warning(f"Could not load '{font_choice}'. Using basic default font.")
|
93 |
+
|
94 |
|
95 |
# --- Text Wrapping ---
|
96 |
drawable_width = IMG_WIDTH - 2 * PADDING_X
|
|
|
187 |
|
188 |
One day, a young girl with eyes as curious as a cat's wandered into his shop. She wasn't interested in the shiny new watches but was drawn to the grandfather clock in the corner. "What's its story?" she asked, her voice soft. Alistair smiled, for he knew he had found the next guardian of the stories. The legacy of the whispering clock would live on."""
|
189 |
|
190 |
+
# Get the list of font names for the dropdown
|
191 |
+
font_choices = ["Arial", "Times New Roman", "Courier New", "DejaVu Sans"]
|
192 |
+
|
193 |
demo = gr.Interface(
|
194 |
fn=text_to_images_and_base64_generator,
|
195 |
inputs=[
|
|
|
198 |
gr.Slider(24, 72, value=48, step=2, label="Font Size"),
|
199 |
gr.Radio(["White", "Light Gray", "Beige", "Pale Blue"], label="Background Color", value="White"),
|
200 |
gr.Radio(["1:1 (Square)", "16:9 (Widescreen)", "9:16 (Vertical)", "4:3 (Standard)"], label="Aspect Ratio", value="1:1 (Square)"),
|
201 |
+
gr.Dropdown(font_choices, label="Font Family", value="Arial")
|
202 |
],
|
203 |
outputs=[
|
204 |
gr.Gallery(label="Generated Images (Preview)", show_label=True, preview=True),
|
|
|
211 |
|
212 |
# --- Main Execution ---
|
213 |
if __name__ == "__main__":
|
214 |
+
# Before launching, check if the fonts directory exists.
|
215 |
+
if not os.path.exists("fonts"):
|
216 |
+
print("---")
|
217 |
+
print("WARNING: 'fonts' directory not found.")
|
218 |
+
print("Please create a 'fonts' directory and add .ttf font files to it.")
|
219 |
+
print("The application will try to use system fonts, but may fail.")
|
220 |
+
print("---")
|
221 |
demo.launch(mcp_server=True)
|