feat: improve AT-SPI labeling
This commit is contained in:
+17
-7
@@ -29,20 +29,30 @@ def resolve_target(saved, current, observation_id, target_id, width, height):
|
||||
return x + w // 2, y + h // 2
|
||||
|
||||
|
||||
def badge_position(bounds, badge_width, badge_height, image_width, image_height):
|
||||
x, y, w, h = bounds
|
||||
# Keep row labels within their own row, never above it in the previous item.
|
||||
left = min(x + 2, max(0, image_width - badge_width))
|
||||
top = min(y + max(0, (h - badge_height) // 2), max(0, image_height - badge_height))
|
||||
return left, top
|
||||
|
||||
|
||||
def annotate(image, targets):
|
||||
from PIL import ImageDraw
|
||||
from PIL import ImageDraw, ImageFont
|
||||
marked = image.copy()
|
||||
draw = ImageDraw.Draw(marked)
|
||||
font = ImageFont.load_default(size=14)
|
||||
for target in targets:
|
||||
x, y, w, h = target["bounds"]
|
||||
draw.rectangle((x, y, x + w - 1, y + h - 1), outline="#ff00cc", width=2)
|
||||
# Draw badges last so another element's outline cannot cross out a number.
|
||||
for target in targets:
|
||||
label = str(target["id"])
|
||||
box = draw.textbbox((0, 0), label)
|
||||
label_width, label_height = box[2] + 6, box[3] - box[1] + 6
|
||||
left = min(x, max(0, image.width - label_width))
|
||||
top = max(0, y - label_height)
|
||||
draw.rectangle((left, top, left + label_width, top + label_height), fill="#ffff00")
|
||||
draw.text((left + 3, top + 3 - box[1]), label, fill="black")
|
||||
box = draw.textbbox((0, 0), label, font=font)
|
||||
label_width, label_height = box[2] - box[0] + 6, box[3] - box[1] + 6
|
||||
left, top = badge_position(target["bounds"], label_width, label_height, image.width, image.height)
|
||||
draw.rectangle((left, top, left + label_width - 1, top + label_height - 1), fill="#ffff00", outline="black")
|
||||
draw.text((left + 3 - box[0], top + 3 - box[1]), label, fill="black", font=font)
|
||||
return marked
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user