Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -35,20 +35,50 @@ def text_behind_image(input_image, text, text_color, font_size, text_opacity):
|
|
35 |
|
36 |
# Determine font size (relative to image size)
|
37 |
font_size = int(min(width, height) * (font_size / 100)) # Convert percentage to actual size
|
|
|
|
|
38 |
|
39 |
# Try to load a nice font, fall back to default if not available
|
40 |
try:
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
except:
|
43 |
font = ImageFont.load_default()
|
44 |
|
|
|
|
|
|
|
|
|
45 |
# Word wrap the text to fit within the image width
|
46 |
margin = 20
|
47 |
-
|
|
|
48 |
word_list = wrapper.wrap(text)
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
# Calculate text block height
|
51 |
-
|
|
|
52 |
|
53 |
# Position text in the center
|
54 |
y_text = (height - text_height) // 2
|
@@ -56,17 +86,40 @@ def text_behind_image(input_image, text, text_color, font_size, text_opacity):
|
|
56 |
# Draw text with specified opacity
|
57 |
for line in word_list:
|
58 |
# Get line width
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
60 |
# Center the line
|
61 |
x_text = (width - line_width) // 2
|
62 |
|
63 |
# Draw text with specified opacity
|
64 |
text_color_with_opacity = text_color_rgb + (int(text_opacity * 255),)
|
65 |
draw.text((x_text, y_text), line, font=font, fill=text_color_with_opacity)
|
66 |
-
y_text +=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
# Remove background from the original image to get the person silhouette
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
# Composite the images: text in background, then person on top
|
72 |
final_image = Image.alpha_composite(background.convert('RGBA'), person_img.convert('RGBA'))
|
@@ -111,4 +164,4 @@ with gr.Blocks(title="Text Behind Image") as demo:
|
|
111 |
gr.Markdown("4. Click 'Generate' to create your image")
|
112 |
|
113 |
# Launch the app
|
114 |
-
demo.launch()
|
|
|
35 |
|
36 |
# Determine font size (relative to image size)
|
37 |
font_size = int(min(width, height) * (font_size / 100)) # Convert percentage to actual size
|
38 |
+
if font_size < 10: # Ensure font is at least 10 pixels
|
39 |
+
font_size = 10
|
40 |
|
41 |
# Try to load a nice font, fall back to default if not available
|
42 |
try:
|
43 |
+
# Try different common fonts
|
44 |
+
font_paths = [
|
45 |
+
"arial.ttf",
|
46 |
+
"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
|
47 |
+
"/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf"
|
48 |
+
]
|
49 |
+
font = None
|
50 |
+
for font_path in font_paths:
|
51 |
+
try:
|
52 |
+
font = ImageFont.truetype(font_path, font_size)
|
53 |
+
break
|
54 |
+
except:
|
55 |
+
continue
|
56 |
+
|
57 |
+
if font is None:
|
58 |
+
font = ImageFont.load_default()
|
59 |
except:
|
60 |
font = ImageFont.load_default()
|
61 |
|
62 |
+
# Prepare the text - make it ALL CAPS for better visual impact
|
63 |
+
text = text.upper()
|
64 |
+
|
65 |
+
# Fill the entire background with repeating text pattern
|
66 |
# Word wrap the text to fit within the image width
|
67 |
margin = 20
|
68 |
+
max_chars = max(10, int(width / (font_size/2)) - 2*margin)
|
69 |
+
wrapper = textwrap.TextWrapper(width=max_chars)
|
70 |
word_list = wrapper.wrap(text)
|
71 |
|
72 |
+
# If text is too short, repeat it to fill the background
|
73 |
+
if len(word_list) == 1 and len(text) < 10:
|
74 |
+
repeated_text = (text + " ") * 10
|
75 |
+
word_list = []
|
76 |
+
for i in range(0, height, font_size + 5):
|
77 |
+
word_list.append(repeated_text)
|
78 |
+
|
79 |
# Calculate text block height
|
80 |
+
line_spacing = int(font_size * 1.2)
|
81 |
+
text_height = len(word_list) * line_spacing
|
82 |
|
83 |
# Position text in the center
|
84 |
y_text = (height - text_height) // 2
|
|
|
86 |
# Draw text with specified opacity
|
87 |
for line in word_list:
|
88 |
# Get line width
|
89 |
+
try:
|
90 |
+
line_width = font.getbbox(line)[2] - font.getbbox(line)[0]
|
91 |
+
except:
|
92 |
+
# Fallback method to estimate width
|
93 |
+
line_width = len(line) * (font_size // 2)
|
94 |
+
|
95 |
# Center the line
|
96 |
x_text = (width - line_width) // 2
|
97 |
|
98 |
# Draw text with specified opacity
|
99 |
text_color_with_opacity = text_color_rgb + (int(text_opacity * 255),)
|
100 |
draw.text((x_text, y_text), line, font=font, fill=text_color_with_opacity)
|
101 |
+
y_text += line_spacing
|
102 |
+
|
103 |
+
# Ensure the text is very visible by drawing it multiple times with different Y positions
|
104 |
+
if len(word_list) < 5:
|
105 |
+
for offset in [-line_spacing*2, line_spacing*2]:
|
106 |
+
y_text = (height - text_height) // 2 + offset
|
107 |
+
for line in word_list:
|
108 |
+
try:
|
109 |
+
line_width = font.getbbox(line)[2] - font.getbbox(line)[0]
|
110 |
+
except:
|
111 |
+
line_width = len(line) * (font_size // 2)
|
112 |
+
x_text = (width - line_width) // 2
|
113 |
+
draw.text((x_text, y_text), line, font=font, fill=text_color_with_opacity)
|
114 |
+
y_text += line_spacing
|
115 |
|
116 |
# Remove background from the original image to get the person silhouette
|
117 |
+
# Use the u2net_human_seg model specifically for better human segmentation
|
118 |
+
try:
|
119 |
+
person_img = remove(img, model_name="u2net_human_seg")
|
120 |
+
except:
|
121 |
+
# Fallback to default model if human_seg not available
|
122 |
+
person_img = remove(img)
|
123 |
|
124 |
# Composite the images: text in background, then person on top
|
125 |
final_image = Image.alpha_composite(background.convert('RGBA'), person_img.convert('RGBA'))
|
|
|
164 |
gr.Markdown("4. Click 'Generate' to create your image")
|
165 |
|
166 |
# Launch the app
|
167 |
+
demo.launch()
|