AMfeta99 commited on
Commit
e2b8f6f
·
verified ·
1 Parent(s): 1b3cd17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -5
app.py CHANGED
@@ -9,17 +9,18 @@ def add_label_to_image(image, label):
9
  # Create a drawing context
10
  draw = ImageDraw.Draw(image)
11
 
12
- # Get the text size and position
13
- text_size = draw.textsize(label)
14
- position = ((image.width - text_size[0]) // 2, image.height - text_size[1] - 10) # Centered at the bottom
 
15
 
16
  # Add a semi-transparent rectangle behind the text for better visibility
17
  rect_margin = 10
18
  rect_position = [
19
  position[0] - rect_margin,
20
  position[1] - rect_margin,
21
- position[0] + text_size[0] + rect_margin,
22
- position[1] + text_size[1] + rect_margin,
23
  ]
24
  draw.rectangle(rect_position, fill=(0, 0, 0, 128)) # Semi-transparent black
25
  draw.text(position, label, fill="white", font=font)
 
9
  # Create a drawing context
10
  draw = ImageDraw.Draw(image)
11
 
12
+ # Calculate the size and position of the text
13
+ text_bbox = draw.textbbox((0, 0), label, font=font)
14
+ text_width, text_height = text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1]
15
+ position = ((image.width - text_width) // 2, image.height - text_height - 10) # Centered at the bottom
16
 
17
  # Add a semi-transparent rectangle behind the text for better visibility
18
  rect_margin = 10
19
  rect_position = [
20
  position[0] - rect_margin,
21
  position[1] - rect_margin,
22
+ position[0] + text_width + rect_margin,
23
+ position[1] + text_height + rect_margin,
24
  ]
25
  draw.rectangle(rect_position, fill=(0, 0, 0, 128)) # Semi-transparent black
26
  draw.text(position, label, fill="white", font=font)