Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
#
|
13 |
-
|
14 |
-
|
|
|
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] +
|
22 |
-
position[1] +
|
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)
|