Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -92,29 +92,41 @@ def text_rendering_generate_image(user_prompt, input_text, text_color, text_size
|
|
92 |
# Try to load a font that supports multilingual text
|
93 |
try:
|
94 |
# Attempt to load a system font that supports multilingual text
|
95 |
-
|
|
|
|
|
96 |
except IOError:
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
# Parse position (top, center, bottom)
|
|
|
101 |
if text_position == "top":
|
102 |
-
position = (width // 2,
|
103 |
elif text_position == "bottom":
|
104 |
-
position = (width // 2, height -
|
105 |
else: # center
|
106 |
position = (width // 2, height // 2)
|
107 |
|
108 |
# Add text with outline for better visibility
|
109 |
-
# Draw text outline (shadow)
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
|
|
|
|
|
|
118 |
|
119 |
# Draw the main text
|
120 |
draw.text(
|
@@ -171,7 +183,7 @@ def load_text_examples():
|
|
171 |
"prompt": "cute character with speech bubble",
|
172 |
"text": "Hello World!",
|
173 |
"color": "#ffffff",
|
174 |
-
"size":
|
175 |
"position": "center",
|
176 |
"filename": "00.jpg",
|
177 |
"height": 680,
|
@@ -182,7 +194,7 @@ def load_text_examples():
|
|
182 |
"prompt": "landscape with message",
|
183 |
"text": "์๋
ํ์ธ์!",
|
184 |
"color": "#ffff00",
|
185 |
-
"size":
|
186 |
"position": "top",
|
187 |
"filename": "03.jpg",
|
188 |
"height": 1024,
|
@@ -193,7 +205,7 @@ def load_text_examples():
|
|
193 |
"prompt": "character with subtitles",
|
194 |
"text": "ใใใซใกใฏไธ็!",
|
195 |
"color": "#00ffff",
|
196 |
-
"size":
|
197 |
"position": "bottom",
|
198 |
"filename": "02.jpg",
|
199 |
"height": 560,
|
@@ -483,11 +495,11 @@ with gr.Blocks(css=css) as demo:
|
|
483 |
)
|
484 |
|
485 |
text_size = gr.Slider(
|
486 |
-
minimum=
|
487 |
-
maximum=
|
488 |
-
step=
|
489 |
label="Text Size",
|
490 |
-
value=
|
491 |
)
|
492 |
|
493 |
text_position = gr.Radio(
|
|
|
92 |
# Try to load a font that supports multilingual text
|
93 |
try:
|
94 |
# Attempt to load a system font that supports multilingual text
|
95 |
+
# Scale up the text size significantly to make it more visible
|
96 |
+
actual_text_size = text_size * 3 # Multiply the size by 3 for better visibility
|
97 |
+
font = ImageFont.truetype("Arial Unicode.ttf", actual_text_size)
|
98 |
except IOError:
|
99 |
+
try:
|
100 |
+
# Try another common font if Arial Unicode is not available
|
101 |
+
actual_text_size = text_size * 3
|
102 |
+
font = ImageFont.truetype("DejaVuSans.ttf", actual_text_size)
|
103 |
+
except IOError:
|
104 |
+
# Final fallback to default font with increased size
|
105 |
+
font = ImageFont.load_default()
|
106 |
|
107 |
# Parse position (top, center, bottom)
|
108 |
+
# Use actual_text_size for position calculations to maintain proper spacing
|
109 |
if text_position == "top":
|
110 |
+
position = (width // 2, actual_text_size + 30) # More padding from the top
|
111 |
elif text_position == "bottom":
|
112 |
+
position = (width // 2, height - actual_text_size - 30) # More padding from the bottom
|
113 |
else: # center
|
114 |
position = (width // 2, height // 2)
|
115 |
|
116 |
# Add text with outline for better visibility
|
117 |
+
# Draw text outline (shadow) with larger offset for better visibility
|
118 |
+
outline_size = max(3, actual_text_size // 15) # Scale outline size with text size
|
119 |
+
for offset_x in range(-outline_size, outline_size + 1, outline_size):
|
120 |
+
for offset_y in range(-outline_size, outline_size + 1, outline_size):
|
121 |
+
if offset_x == 0 and offset_y == 0:
|
122 |
+
continue # Skip the center position (will be drawn as main text)
|
123 |
+
draw.text(
|
124 |
+
(position[0] + offset_x, position[1] + offset_y),
|
125 |
+
input_text,
|
126 |
+
fill="black",
|
127 |
+
font=font,
|
128 |
+
anchor="mm" # Center align the text
|
129 |
+
)
|
130 |
|
131 |
# Draw the main text
|
132 |
draw.text(
|
|
|
183 |
"prompt": "cute character with speech bubble",
|
184 |
"text": "Hello World!",
|
185 |
"color": "#ffffff",
|
186 |
+
"size": 72,
|
187 |
"position": "center",
|
188 |
"filename": "00.jpg",
|
189 |
"height": 680,
|
|
|
194 |
"prompt": "landscape with message",
|
195 |
"text": "์๋
ํ์ธ์!",
|
196 |
"color": "#ffff00",
|
197 |
+
"size": 100,
|
198 |
"position": "top",
|
199 |
"filename": "03.jpg",
|
200 |
"height": 1024,
|
|
|
205 |
"prompt": "character with subtitles",
|
206 |
"text": "ใใใซใกใฏไธ็!",
|
207 |
"color": "#00ffff",
|
208 |
+
"size": 90,
|
209 |
"position": "bottom",
|
210 |
"filename": "02.jpg",
|
211 |
"height": 560,
|
|
|
495 |
)
|
496 |
|
497 |
text_size = gr.Slider(
|
498 |
+
minimum=24,
|
499 |
+
maximum=200,
|
500 |
+
step=4,
|
501 |
label="Text Size",
|
502 |
+
value=72
|
503 |
)
|
504 |
|
505 |
text_position = gr.Radio(
|