Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,47 +4,73 @@ 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
|
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 =
|
24 |
-
|
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 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
try:
|
36 |
-
font = ImageFont.
|
37 |
except:
|
|
|
38 |
font = ImageFont.load_default()
|
39 |
|
40 |
-
# Calculate text area
|
41 |
text_width = img_width - (padding * 2)
|
42 |
|
43 |
-
#
|
44 |
-
|
45 |
-
|
|
|
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)
|
@@ -57,14 +83,15 @@ def text_to_image(text_content):
|
|
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 |
|
@@ -72,15 +99,22 @@ def text_to_image(text_content):
|
|
72 |
|
73 |
demo = gr.Interface(
|
74 |
fn=text_to_image,
|
75 |
-
inputs=[
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
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.
|
84 |
)
|
85 |
|
86 |
if __name__ == "__main__":
|
|
|
4 |
import io
|
5 |
import os
|
6 |
|
7 |
+
def text_to_image(text_content, font_size_option):
|
8 |
"""
|
9 |
+
Convert text content to a formatted image with customizable font size.
|
10 |
Args:
|
11 |
text_content (str): The input text to convert to image
|
12 |
+
font_size_option (str): Font size option (Small, Medium, Large)
|
13 |
Returns:
|
14 |
PIL.Image: Generated image
|
15 |
"""
|
16 |
if not text_content.strip():
|
17 |
text_content = "Please enter some text to convert to image."
|
18 |
|
19 |
+
# Font size mapping
|
20 |
+
font_sizes = {
|
21 |
+
"Small": 20,
|
22 |
+
"Medium": 28,
|
23 |
+
"Large": 36
|
24 |
+
}
|
25 |
+
font_size = font_sizes.get(font_size_option, 28)
|
26 |
+
|
27 |
# Fixed styling parameters
|
28 |
img_width = 600 # 3:4 ratio width
|
29 |
img_height = 800 # 3:4 ratio height
|
30 |
background_color = "#F8F9FA" # Light theme background
|
31 |
text_color = "#000000" # Black text
|
32 |
+
padding = 50
|
33 |
+
line_spacing = font_size // 3 # Dynamic line spacing based on font size
|
|
|
34 |
|
35 |
+
# Create image with higher quality
|
36 |
img = Image.new('RGB', (img_width, img_height), background_color)
|
37 |
draw = ImageDraw.Draw(img)
|
38 |
|
39 |
+
# Try to use a nice font with better quality, fallback to default if not available
|
40 |
+
font = None
|
41 |
+
font_paths = [
|
42 |
+
"/System/Library/Fonts/Helvetica.ttc", # macOS
|
43 |
+
"/System/Library/Fonts/Arial.ttf", # macOS
|
44 |
+
"/Windows/Fonts/arial.ttf", # Windows
|
45 |
+
"/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf", # Linux
|
46 |
+
"arial.ttf"
|
47 |
+
]
|
48 |
+
|
49 |
+
for font_path in font_paths:
|
50 |
+
try:
|
51 |
+
font = ImageFont.truetype(font_path, font_size)
|
52 |
+
break
|
53 |
+
except:
|
54 |
+
continue
|
55 |
+
|
56 |
+
if font is None:
|
57 |
try:
|
58 |
+
font = ImageFont.load_default()
|
59 |
except:
|
60 |
+
# Fallback font
|
61 |
font = ImageFont.load_default()
|
62 |
|
63 |
+
# Calculate text area with better precision
|
64 |
text_width = img_width - (padding * 2)
|
65 |
|
66 |
+
# More accurate text wrapping based on actual character width
|
67 |
+
# Test character width
|
68 |
+
test_char_width = draw.textbbox((0, 0), "W", font=font)[2]
|
69 |
+
chars_per_line = max(1, text_width // test_char_width)
|
70 |
|
71 |
+
wrapped_lines = []
|
72 |
paragraphs = text_content.split('\n')
|
73 |
+
|
74 |
for paragraph in paragraphs:
|
75 |
if paragraph.strip():
|
76 |
lines = textwrap.wrap(paragraph, width=chars_per_line)
|
|
|
83 |
while wrapped_lines and wrapped_lines[-1] == "":
|
84 |
wrapped_lines.pop()
|
85 |
|
86 |
+
# Draw text with better positioning
|
87 |
y_position = padding
|
88 |
for line in wrapped_lines:
|
89 |
+
if y_position + font_size + line_spacing > img_height - padding:
|
90 |
# Add "..." if text is too long
|
91 |
draw.text((padding, y_position), "...", font=font, fill=text_color)
|
92 |
break
|
93 |
|
94 |
+
# Use textbbox for better text positioning
|
95 |
draw.text((padding, y_position), line, font=font, fill=text_color)
|
96 |
y_position += font_size + line_spacing
|
97 |
|
|
|
99 |
|
100 |
demo = gr.Interface(
|
101 |
fn=text_to_image,
|
102 |
+
inputs=[
|
103 |
+
gr.Textbox(
|
104 |
+
label="Text Content",
|
105 |
+
placeholder="Enter your text content here...",
|
106 |
+
lines=10,
|
107 |
+
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, choose your preferred font size, and click generate!"
|
108 |
+
),
|
109 |
+
gr.Radio(
|
110 |
+
choices=["Small", "Medium", "Large"],
|
111 |
+
value="Medium",
|
112 |
+
label="Font Size"
|
113 |
+
)
|
114 |
+
],
|
115 |
outputs=[gr.Image(label="Generated Image", type="pil")],
|
116 |
title="Text to Image Converter",
|
117 |
+
description="Convert your text content into attractive images with a clean, readable design. Choose your font size and generate high-quality images perfect for social media!"
|
118 |
)
|
119 |
|
120 |
if __name__ == "__main__":
|