Skip to content

Instantly share code, notes, and snippets.

@gguuss
Created November 19, 2019 11:38
Show Gist options
  • Save gguuss/d7c7814e5bc7fdf8ee3fc32d5d06d6ea to your computer and use it in GitHub Desktop.
Save gguuss/d7c7814e5bc7fdf8ee3fc32d5d06d6ea to your computer and use it in GitHub Desktop.
Label and draw a bounding box around an image target
def draw_obj(image_file, text, vects):
"""Draw a border around the image using the label and vector list."""
from PIL import Image, ImageDraw, ImageFont
im = Image.open(image_file)
w = im.width
h = im.height
draw = ImageDraw.Draw(im)
fnt = ImageFont.truetype('Verdana.ttf', 40)
draw.text(
(vects[0].x * w, vects[0].y*h),
text,
font=fnt,
fill=(255,255,0,255))
print('1111')
draw.polygon([
vects[0].x * w, vects[0].y * h,
vects[1].x * w, vects[1].y * h,
vects[2].x * w, vects[2].y * h,
vects[3].x * w, vects[3].y * h], None, 'red')
im.save(image_file, 'JPEG')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment