File size: 931 Bytes
7c11918 2007b83 7c11918 2007b83 7c11918 2007b83 7c11918 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
from PIL import Image, ImageDraw, ImageFont
def draw_bboxes(img, bboxes):
"""
Draw bounding boxes on an image.
Args:
- image_path (str): Path to the image file.
- bboxes (list of lists/tuples): Bounding boxes with [x_min, y_min, x_max, y_max, class_id].
"""
# Load an image
draw = ImageDraw.Draw(img)
# Font for class_id (optional)
try:
font = ImageFont.truetype("arial.ttf", 30)
except IOError:
font = ImageFont.load_default(30)
width, height = img.size
for bbox in bboxes:
class_id, x_min, y_min, x_max, y_max = bbox
x_min = x_min * width
x_max = x_max * width
y_min = y_min * height
y_max = y_max * height
shape = [(x_min, y_min), (x_max, y_max)]
draw.rectangle(shape, outline="red", width=2)
draw.text((x_min, y_min), str(int(class_id)), font=font, fill="blue")
img.save("output.jpg")
|