Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -9,10 +9,18 @@ def add_label_to_image(image, label):
|
|
9 |
# Create a drawing context
|
10 |
draw = ImageDraw.Draw(image)
|
11 |
|
12 |
-
#
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
text_width, text_height = text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1]
|
15 |
-
position = (
|
16 |
|
17 |
# Add a semi-transparent rectangle behind the text for better visibility
|
18 |
rect_margin = 10
|
@@ -23,9 +31,10 @@ def add_label_to_image(image, label):
|
|
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")
|
27 |
return image
|
28 |
|
|
|
29 |
# Function to plot, label, and save an image
|
30 |
def plot_and_save_agent_image(agent_image, label, save_path=None):
|
31 |
# Convert AgentImage to a raw PIL Image
|
|
|
9 |
# Create a drawing context
|
10 |
draw = ImageDraw.Draw(image)
|
11 |
|
12 |
+
# Define font size and color (adjust font path for your environment)
|
13 |
+
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" # Example font path
|
14 |
+
font_size = 60 # Larger font size for better visibility
|
15 |
+
try:
|
16 |
+
font = ImageFont.truetype(font_path, font_size)
|
17 |
+
except:
|
18 |
+
font = ImageFont.load_default()
|
19 |
+
|
20 |
+
# Calculate the size and position of the text (aligned to the left)
|
21 |
+
text_bbox = draw.textbbox((0, 0), label, font=font)
|
22 |
text_width, text_height = text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1]
|
23 |
+
position = (20, image.height - text_height - 20) # Left-aligned with margin
|
24 |
|
25 |
# Add a semi-transparent rectangle behind the text for better visibility
|
26 |
rect_margin = 10
|
|
|
31 |
position[1] + text_height + rect_margin,
|
32 |
]
|
33 |
draw.rectangle(rect_position, fill=(0, 0, 0, 128)) # Semi-transparent black
|
34 |
+
draw.text(position, label, fill="white", font=font)
|
35 |
return image
|
36 |
|
37 |
+
|
38 |
# Function to plot, label, and save an image
|
39 |
def plot_and_save_agent_image(agent_image, label, save_path=None):
|
40 |
# Convert AgentImage to a raw PIL Image
|