ysharma HF staff commited on
Commit
69751c5
1 Parent(s): cd3441f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -40
app.py CHANGED
@@ -8,25 +8,12 @@ import uuid
8
 
9
  client = Client("ysharma/BiRefNet_for_text_writing")
10
 
11
- def add_text_with_effects(draw, text, x, y, font, text_color, stroke_width, shadow=False, shadow_offset=(5, 5)):
12
- """Helper function to draw text with stroke and optional shadow"""
13
- # Draw shadow first if enabled
14
- if shadow:
15
- # Create shadow color (darker version of text color or black)
16
- shadow_color = (0, 0, 0, text_color[3]) # Black with same opacity as text
17
-
18
- # Draw the shadow text with stroke effect
19
- shadow_x = x + shadow_offset[0]
20
- shadow_y = y + shadow_offset[1]
21
-
22
- for adj_x in range(-stroke_width, stroke_width + 1):
23
- for adj_y in range(-stroke_width, stroke_width + 1):
24
- draw.text(
25
- (shadow_x + adj_x, shadow_y + adj_y),
26
- text,
27
- font=font,
28
- fill=shadow_color
29
- )
30
 
31
  def remove_background(image):
32
  # Save the image to a specific location
@@ -53,8 +40,7 @@ def add_text_to_image(
53
  opacity,
54
  x_position,
55
  y_position,
56
- thickness,
57
- use_shadow
58
  ):
59
  """
60
  Add text to an image with customizable properties
@@ -106,20 +92,15 @@ def add_text_to_image(
106
  # Create final color with opacity
107
  text_color = (*rgb_color, int(opacity))
108
 
109
- # Calculate shadow offset based on font size
110
- shadow_offset = (int(font_size * 0.05), int(font_size * 0.05))
111
-
112
- # Draw the text with effects
113
- add_text_with_effects(
114
  draw,
115
  text,
116
  actual_x,
117
  actual_y,
118
  font,
119
  text_color,
120
- int(thickness),
121
- shadow=use_shadow,
122
- shadow_offset=shadow_offset
123
  )
124
 
125
  # Combine the original image with the text overlay
@@ -162,7 +143,6 @@ def create_interface():
162
  label="X Position (%)")
163
  y_position = gr.Slider(minimum=0, maximum=100, value=50, step=1,
164
  label="Y Position (%)")
165
- shadow_checkbox = gr.Checkbox(label="Add Shadow Effect", value=False)
166
 
167
  with gr.Column():
168
  # Output image
@@ -182,8 +162,7 @@ def create_interface():
182
  opacity_slider,
183
  x_position,
184
  y_position,
185
- thickness,
186
- shadow_checkbox
187
  ],
188
  outputs=output_image
189
  )
@@ -199,8 +178,7 @@ def create_interface():
199
  150,
200
  50,
201
  21,
202
- 9,
203
- True
204
  ],
205
  [
206
  "pear.jpg",
@@ -210,8 +188,7 @@ def create_interface():
210
  100,
211
  50,
212
  2,
213
- 5,
214
- True
215
  ],
216
  [
217
  "sample_text_image.jpeg",
@@ -221,8 +198,7 @@ def create_interface():
221
  150,
222
  50,
223
  2,
224
- 8,
225
- True
226
  ],
227
  ],
228
  inputs=[
@@ -233,8 +209,7 @@ def create_interface():
233
  opacity_slider,
234
  x_position,
235
  y_position,
236
- thickness,
237
- shadow_checkbox,
238
  ],
239
  outputs=output_image,
240
  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
 
40
  opacity,
41
  x_position,
42
  y_position,
43
+ thickness
 
44
  ):
45
  """
46
  Add text to an image with customizable properties
 
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
 
143
  label="X Position (%)")
144
  y_position = gr.Slider(minimum=0, maximum=100, value=50, step=1,
145
  label="Y Position (%)")
 
146
 
147
  with gr.Column():
148
  # Output image
 
162
  opacity_slider,
163
  x_position,
164
  y_position,
165
+ thickness
 
166
  ],
167
  outputs=output_image
168
  )
 
178
  150,
179
  50,
180
  21,
181
+ 9
 
182
  ],
183
  [
184
  "pear.jpg",
 
188
  100,
189
  50,
190
  2,
191
+ 5
 
192
  ],
193
  [
194
  "sample_text_image.jpeg",
 
198
  150,
199
  50,
200
  2,
201
+ 8
 
202
  ],
203
  ],
204
  inputs=[
 
209
  opacity_slider,
210
  x_position,
211
  y_position,
212
+ thickness
 
213
  ],
214
  outputs=output_image,
215
  fn=add_text_to_image,