4
0

More than 3 years have passed since last update.

PILでBoundingBoxを表示するスクリプト例

Last updated at Posted at 2020-01-06

何をしたいか

メモ。

更新

  • 20200116
    • y座標が0に近い時でも画面内にラベルを表示できるようにした

結果のBBをPIL画像に表示する関数

def show_bb(img, x, y, w, h, text, textcolor, bbcolor):
    draw = ImageDraw.Draw(img)
    text_w, text_h = draw.textsize(text)
    label_y = y if y <= text_h else y - text_h
    draw.rectangle((x, label_y, x+w, label_y+h), outline=bbcolor)
    draw.rectangle((x, label_y, x+text_w, label_y+text_h), outline=bbcolor, fill=bbcolor)
    draw.text((x, label_y), text, fill=textcolor)

サンプル

lena_std.jpg

lena_eye.jpg

from PIL import Image, ImageDraw

img = Image.open(path_to_image)
show_bb(img, 244, 249, 47, 30, "eye", (255, 255, 255), (255, 0, 0))
img.show()
4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0