def draw_image(pic_name, boxes, namelist_file):
name_list = get_names_from_file(namelist_file)
color_list = get_color_from_file('ink.color')
im = Image.open(pic_name)
draw = ImageDraw.Draw(im)
lena = mpimg.imread(pic_name)
height, width = lena.shape[:2]
for box in boxes:
x = box.rect.corner.x
y = box.rect.corner.y
w = box.rect.width
h = box.rect.height
left = (x - w / 2) * width
right = (x + w / 2) * width
top = (y - h / 2) * height
bot = (y + h / 2) * height
if left < 0:
left = 0
if right > width - 1:
right = width - 1
if top < 0:
top = 0
if bot > height - 1:
bot = height - 1
category = name_list[box.category]
color = color_list[box.category % color_list.__len__()]
draw.line((left, top, right, top), fill=color, width=5)
draw.line((right, top, right, bot), fill=color, width=5)
draw.line((left, top, left, bot), fill=color, width=5)
draw.line((left, bot, right, bot), fill=color, width=5)
font_size = 20
my_font = ImageFont.truetype("/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-M.ttf", size=font_size)
draw.text([left + 5, top], category, font=my_font, fill=color)
im.show()
评论列表
文章目录