Skip to content

Instantly share code, notes, and snippets.

@aplz
Last active February 3, 2025 23:46
Show Gist options
  • Save aplz/fd34707deffb208f367808aade7e5d5c to your computer and use it in GitHub Desktop.
Save aplz/fd34707deffb208f367808aade7e5d5c to your computer and use it in GitHub Desktop.

Revisions

  1. aplz revised this gist Nov 28, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion draw_text_with_background_opencv.py
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@
    text_offset_x = 10
    text_offset_y = img.shape[0] - 25
    # make the coords of the box with a small padding of two pixels
    box_coords = ((text_offset_x, text_offset_y), (text_offset_x + text_width - 2, text_offset_y - text_height - 2))
    box_coords = ((text_offset_x, text_offset_y), (text_offset_x + text_width + 2, text_offset_y - text_height - 2))
    cv2.rectangle(img, box_coords[0], box_coords[1], rectangle_bgr, cv2.FILLED)
    cv2.putText(img, text, (text_offset_x, text_offset_y), font, fontScale=font_scale, color=(0, 0, 0), thickness=1)
    cv2.imshow("A box!", img)
  2. aplz created this gist Jun 4, 2018.
    23 changes: 23 additions & 0 deletions draw_text_with_background_opencv.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    import cv2 # opencv
    import numpy as np

    font_scale = 1.5
    font = cv2.FONT_HERSHEY_PLAIN

    # set the rectangle background to white
    rectangle_bgr = (255, 255, 255)
    # make a black image
    img = np.zeros((500, 500))
    # set some text
    text = "Some text in a box!"
    # get the width and height of the text box
    (text_width, text_height) = cv2.getTextSize(text, font, fontScale=font_scale, thickness=1)[0]
    # set the text start position
    text_offset_x = 10
    text_offset_y = img.shape[0] - 25
    # make the coords of the box with a small padding of two pixels
    box_coords = ((text_offset_x, text_offset_y), (text_offset_x + text_width - 2, text_offset_y - text_height - 2))
    cv2.rectangle(img, box_coords[0], box_coords[1], rectangle_bgr, cv2.FILLED)
    cv2.putText(img, text, (text_offset_x, text_offset_y), font, fontScale=font_scale, color=(0, 0, 0), thickness=1)
    cv2.imshow("A box!", img)
    cv2.waitKey(0)