Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,86 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
def
|
4 |
"""
|
5 |
-
|
6 |
Args:
|
7 |
-
|
8 |
-
letter (str): The letter to search for
|
9 |
Returns:
|
10 |
-
|
11 |
"""
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
demo = gr.Interface(
|
18 |
-
fn=
|
19 |
-
inputs=[gr.Textbox(
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
23 |
)
|
24 |
|
25 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
2 |
+
from PIL import Image, ImageDraw, ImageFont
|
3 |
+
import textwrap
|
4 |
+
import io
|
5 |
+
import os
|
6 |
|
7 |
+
def text_to_image(text_content):
|
8 |
"""
|
9 |
+
Convert text content to a formatted image with fixed styling.
|
10 |
Args:
|
11 |
+
text_content (str): The input text to convert to image
|
|
|
12 |
Returns:
|
13 |
+
PIL.Image: Generated image
|
14 |
"""
|
15 |
+
if not text_content.strip():
|
16 |
+
text_content = "Please enter some text to convert to image."
|
17 |
+
|
18 |
+
# Fixed styling parameters
|
19 |
+
img_width = 600 # 3:4 ratio width
|
20 |
+
img_height = 800 # 3:4 ratio height
|
21 |
+
background_color = "#F8F9FA" # Light theme background
|
22 |
+
text_color = "#000000" # Black text
|
23 |
+
padding = 40
|
24 |
+
font_size = 24 # Medium font size
|
25 |
+
line_spacing = 8
|
26 |
+
|
27 |
+
# Create image
|
28 |
+
img = Image.new('RGB', (img_width, img_height), background_color)
|
29 |
+
draw = ImageDraw.Draw(img)
|
30 |
+
|
31 |
+
# Try to use a nice font, fallback to default if not available
|
32 |
+
try:
|
33 |
+
font = ImageFont.truetype("/System/Library/Fonts/Arial.ttf", font_size)
|
34 |
+
except:
|
35 |
+
try:
|
36 |
+
font = ImageFont.truetype("arial.ttf", font_size)
|
37 |
+
except:
|
38 |
+
font = ImageFont.load_default()
|
39 |
+
|
40 |
+
# Calculate text area
|
41 |
+
text_width = img_width - (padding * 2)
|
42 |
+
|
43 |
+
# Wrap text to fit width
|
44 |
+
chars_per_line = text_width // (font_size // 2) # Rough estimate
|
45 |
+
wrapped_lines = []
|
46 |
+
|
47 |
+
paragraphs = text_content.split('\n')
|
48 |
+
for paragraph in paragraphs:
|
49 |
+
if paragraph.strip():
|
50 |
+
lines = textwrap.wrap(paragraph, width=chars_per_line)
|
51 |
+
wrapped_lines.extend(lines)
|
52 |
+
wrapped_lines.append("") # Add space between paragraphs
|
53 |
+
else:
|
54 |
+
wrapped_lines.append("")
|
55 |
+
|
56 |
+
# Remove trailing empty lines
|
57 |
+
while wrapped_lines and wrapped_lines[-1] == "":
|
58 |
+
wrapped_lines.pop()
|
59 |
+
|
60 |
+
# Draw text
|
61 |
+
y_position = padding
|
62 |
+
for line in wrapped_lines:
|
63 |
+
if y_position + font_size > img_height - padding:
|
64 |
+
# Add "..." if text is too long
|
65 |
+
draw.text((padding, y_position), "...", font=font, fill=text_color)
|
66 |
+
break
|
67 |
+
|
68 |
+
draw.text((padding, y_position), line, font=font, fill=text_color)
|
69 |
+
y_position += font_size + line_spacing
|
70 |
+
|
71 |
+
return img
|
72 |
|
73 |
demo = gr.Interface(
|
74 |
+
fn=text_to_image,
|
75 |
+
inputs=[gr.Textbox(
|
76 |
+
label="Text Content",
|
77 |
+
placeholder="Enter your text content here...",
|
78 |
+
lines=10,
|
79 |
+
value="Welcome to Text-to-Image Converter!\n\nThis tool converts your text into beautifully formatted images perfect for social media posts.\n\nSimply replace this text with your content and click generate!"
|
80 |
+
)],
|
81 |
+
outputs=[gr.Image(label="Generated Image", type="pil")],
|
82 |
+
title="Text to Image Converter",
|
83 |
+
description="Convert your text content into attractive images with a clean, readable design. Perfect for social media posts and content sharing!"
|
84 |
)
|
85 |
|
86 |
if __name__ == "__main__":
|