Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -56,8 +56,8 @@ def text_behind_image(input_image, text, text_color, font_size, text_opacity):
|
|
56 |
if not text:
|
57 |
text = "SAMPLE TEXT"
|
58 |
|
59 |
-
# Set up the font
|
60 |
-
font_size = max(
|
61 |
|
62 |
# Try several fonts
|
63 |
font = None
|
@@ -80,55 +80,38 @@ def text_behind_image(input_image, text, text_color, font_size, text_opacity):
|
|
80 |
|
81 |
if font is None:
|
82 |
font = ImageFont.load_default()
|
83 |
-
|
84 |
-
#
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
#
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
text_height = len(lines) * line_height
|
113 |
-
|
114 |
-
# Calculate starting Y position (centered)
|
115 |
-
start_y = (height - text_height) // 2
|
116 |
-
|
117 |
-
# Draw each line
|
118 |
-
for i, line in enumerate(lines):
|
119 |
-
# Estimate line width (better than using font.getbbox which can fail)
|
120 |
-
line_width = min(max_width, len(line) * int(font_size * 0.6))
|
121 |
-
|
122 |
-
# Center the line
|
123 |
-
x = (width - line_width) // 2
|
124 |
-
y = start_y + (i * line_height)
|
125 |
-
|
126 |
-
# Draw the text with opacity
|
127 |
-
draw.text((x, y), line, font=font, fill=text_color_rgb + (int(text_opacity * 255),))
|
128 |
|
129 |
# Step 5: Composite all layers together
|
130 |
# First, composite the text on top of the background
|
131 |
-
# We use the inverted mask so text only shows where the person isn't
|
132 |
background_with_text = Image.alpha_composite(background, text_layer)
|
133 |
|
134 |
# Then, composite the person on top
|
|
|
56 |
if not text:
|
57 |
text = "SAMPLE TEXT"
|
58 |
|
59 |
+
# Set up the font size - make it larger since we're just placing it once
|
60 |
+
font_size = max(20, int(min(width, height) * (font_size / 100)))
|
61 |
|
62 |
# Try several fonts
|
63 |
font = None
|
|
|
80 |
|
81 |
if font is None:
|
82 |
font = ImageFont.load_default()
|
83 |
+
|
84 |
+
# Get text dimensions
|
85 |
+
# Try to use getbbox first for newer PIL versions
|
86 |
+
try:
|
87 |
+
text_width = font.getbbox(text)[2] - font.getbbox(text)[0]
|
88 |
+
text_height = font.getbbox(text)[3] - font.getbbox(text)[1]
|
89 |
+
except:
|
90 |
+
# Fallback for older PIL versions
|
91 |
+
try:
|
92 |
+
text_width = font.getsize(text)[0]
|
93 |
+
text_height = font.getsize(text)[1]
|
94 |
+
except:
|
95 |
+
# Second fallback - estimate size
|
96 |
+
text_width = len(text) * (font_size * 0.6)
|
97 |
+
text_height = font_size * 1.2
|
98 |
+
|
99 |
+
# Position in the upper middle section
|
100 |
+
# Divide the image into a 3x3 grid and put the text in the top-center cell
|
101 |
+
grid_width = width // 3
|
102 |
+
grid_height = height // 3
|
103 |
+
|
104 |
+
# Center horizontally in the middle third
|
105 |
+
x_center = width // 2 - text_width // 2
|
106 |
+
|
107 |
+
# Position vertically in the top third
|
108 |
+
y_top = grid_height // 2 - text_height // 2
|
109 |
+
|
110 |
+
# Draw the text with specified opacity and color
|
111 |
+
draw.text((x_center, y_top), text, font=font, fill=text_color_rgb + (int(text_opacity * 255),))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
# Step 5: Composite all layers together
|
114 |
# First, composite the text on top of the background
|
|
|
115 |
background_with_text = Image.alpha_composite(background, text_layer)
|
116 |
|
117 |
# Then, composite the person on top
|