Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,13 @@ import uuid
|
|
| 8 |
|
| 9 |
client = Client("ysharma/BiRefNet_for_text_writing")
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def remove_background(image):
|
| 12 |
# Save the image to a specific location
|
| 13 |
filename = f"image_{uuid.uuid4()}.png" # Generates a universally unique identifier (UUID) for the filename
|
|
@@ -25,7 +32,6 @@ def superimpose(image_with_text, overlay_image):
|
|
| 25 |
# image_with_text.save("output_image.png")
|
| 26 |
return image_with_text
|
| 27 |
|
| 28 |
-
|
| 29 |
def add_text_to_image(
|
| 30 |
input_image,
|
| 31 |
text,
|
|
@@ -33,7 +39,8 @@ def add_text_to_image(
|
|
| 33 |
color,
|
| 34 |
opacity,
|
| 35 |
x_position,
|
| 36 |
-
y_position
|
|
|
|
| 37 |
):
|
| 38 |
"""
|
| 39 |
Add text to an image with customizable properties
|
|
@@ -82,12 +89,18 @@ def add_text_to_image(
|
|
| 82 |
actual_x = int((image.width - text_width) * (x_position / 100))
|
| 83 |
actual_y = int((image.height - text_height) * (y_position / 100))
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
)
|
| 92 |
|
| 93 |
# Combine the original image with the text overlay
|
|
@@ -117,6 +130,8 @@ def create_interface():
|
|
| 117 |
text_input = gr.Textbox(label="Enter Text", placeholder="Type your text here...")
|
| 118 |
font_size = gr.Slider(minimum=10, maximum=800, value=400, step=10,
|
| 119 |
label="Font Size")
|
|
|
|
|
|
|
| 120 |
color_dropdown = gr.Dropdown(
|
| 121 |
choices=["White", "Black", "Red", "Green", "Blue", "Yellow", "Purple"],
|
| 122 |
value="White",
|
|
@@ -146,7 +161,8 @@ def create_interface():
|
|
| 146 |
color_dropdown,
|
| 147 |
opacity_slider,
|
| 148 |
x_position,
|
| 149 |
-
y_position
|
|
|
|
| 150 |
],
|
| 151 |
outputs=output_image
|
| 152 |
)
|
|
@@ -161,7 +177,8 @@ def create_interface():
|
|
| 161 |
"Purple",
|
| 162 |
150,
|
| 163 |
50,
|
| 164 |
-
21
|
|
|
|
| 165 |
],
|
| 166 |
[
|
| 167 |
"pear.jpg",
|
|
@@ -170,7 +187,8 @@ def create_interface():
|
|
| 170 |
"Black",
|
| 171 |
100,
|
| 172 |
50,
|
| 173 |
-
2
|
|
|
|
| 174 |
],
|
| 175 |
[
|
| 176 |
"sample_text_image.jpeg",
|
|
@@ -179,7 +197,8 @@ def create_interface():
|
|
| 179 |
"Black",
|
| 180 |
150,
|
| 181 |
50,
|
| 182 |
-
2
|
|
|
|
| 183 |
],
|
| 184 |
],
|
| 185 |
inputs=[
|
|
@@ -189,7 +208,8 @@ def create_interface():
|
|
| 189 |
color_dropdown,
|
| 190 |
opacity_slider,
|
| 191 |
x_position,
|
| 192 |
-
y_position
|
|
|
|
| 193 |
],
|
| 194 |
outputs=output_image,
|
| 195 |
fn=add_text_to_image,
|
|
|
|
| 8 |
|
| 9 |
client = Client("ysharma/BiRefNet_for_text_writing")
|
| 10 |
|
| 11 |
+
def add_text_with_stroke(draw, text, x, y, font, text_color, stroke_width):
|
| 12 |
+
"""Helper function to draw text with stroke"""
|
| 13 |
+
# Draw the stroke/outline
|
| 14 |
+
for adj_x in range(-stroke_width, stroke_width + 1):
|
| 15 |
+
for adj_y in range(-stroke_width, stroke_width + 1):
|
| 16 |
+
draw.text((x + adj_x, y + adj_y), text, font=font, fill=text_color)
|
| 17 |
+
|
| 18 |
def remove_background(image):
|
| 19 |
# Save the image to a specific location
|
| 20 |
filename = f"image_{uuid.uuid4()}.png" # Generates a universally unique identifier (UUID) for the filename
|
|
|
|
| 32 |
# image_with_text.save("output_image.png")
|
| 33 |
return image_with_text
|
| 34 |
|
|
|
|
| 35 |
def add_text_to_image(
|
| 36 |
input_image,
|
| 37 |
text,
|
|
|
|
| 39 |
color,
|
| 40 |
opacity,
|
| 41 |
x_position,
|
| 42 |
+
y_position,
|
| 43 |
+
thickness
|
| 44 |
):
|
| 45 |
"""
|
| 46 |
Add text to an image with customizable properties
|
|
|
|
| 89 |
actual_x = int((image.width - text_width) * (x_position / 100))
|
| 90 |
actual_y = int((image.height - text_height) * (y_position / 100))
|
| 91 |
|
| 92 |
+
# Create final color with opacity
|
| 93 |
+
text_color = (*rgb_color, int(opacity))
|
| 94 |
+
|
| 95 |
+
# Draw the text with stroke for thickness
|
| 96 |
+
add_text_with_stroke(
|
| 97 |
+
draw,
|
| 98 |
+
text,
|
| 99 |
+
actual_x,
|
| 100 |
+
actual_y,
|
| 101 |
+
font,
|
| 102 |
+
text_color,
|
| 103 |
+
int(thickness)
|
| 104 |
)
|
| 105 |
|
| 106 |
# Combine the original image with the text overlay
|
|
|
|
| 130 |
text_input = gr.Textbox(label="Enter Text", placeholder="Type your text here...")
|
| 131 |
font_size = gr.Slider(minimum=10, maximum=800, value=400, step=10,
|
| 132 |
label="Font Size")
|
| 133 |
+
thickness = gr.Slider(minimum=0, maximum=20, value=0, step=1,
|
| 134 |
+
label="Text Thickness")
|
| 135 |
color_dropdown = gr.Dropdown(
|
| 136 |
choices=["White", "Black", "Red", "Green", "Blue", "Yellow", "Purple"],
|
| 137 |
value="White",
|
|
|
|
| 161 |
color_dropdown,
|
| 162 |
opacity_slider,
|
| 163 |
x_position,
|
| 164 |
+
y_position,
|
| 165 |
+
thickness
|
| 166 |
],
|
| 167 |
outputs=output_image
|
| 168 |
)
|
|
|
|
| 177 |
"Purple",
|
| 178 |
150,
|
| 179 |
50,
|
| 180 |
+
21,
|
| 181 |
+
9
|
| 182 |
],
|
| 183 |
[
|
| 184 |
"pear.jpg",
|
|
|
|
| 187 |
"Black",
|
| 188 |
100,
|
| 189 |
50,
|
| 190 |
+
2,
|
| 191 |
+
5
|
| 192 |
],
|
| 193 |
[
|
| 194 |
"sample_text_image.jpeg",
|
|
|
|
| 197 |
"Black",
|
| 198 |
150,
|
| 199 |
50,
|
| 200 |
+
2,
|
| 201 |
+
8
|
| 202 |
],
|
| 203 |
],
|
| 204 |
inputs=[
|
|
|
|
| 208 |
color_dropdown,
|
| 209 |
opacity_slider,
|
| 210 |
x_position,
|
| 211 |
+
y_position,
|
| 212 |
+
thickness
|
| 213 |
],
|
| 214 |
outputs=output_image,
|
| 215 |
fn=add_text_to_image,
|