Skip to content

Instantly share code, notes, and snippets.

@aplz
Last active September 3, 2023 22:56
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.
draw text with background // opencv
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)
@haghad
Copy link

haghad commented Mar 25, 2019

This code helped me, thank you.
It is especially helpful that this code runs 100% independently of any other code.

@norahsakal
Copy link

Thanks for sharing this, really helped me!

@kindofausername
Copy link

Very nice, thank you a lot�.

@Yanghojun
Copy link

Thank you very much ^^

@zhuqiangumd
Copy link

Thanks for your great code!

@choowilson
Copy link

@SAmmarAbbas
Copy link

There is a small bug which becomes prominent if you increase the padding.
On line 19, it should be: text_offset_x + text_width + 2

@aplz
Copy link
Author

aplz commented Nov 28, 2019

There is a small bug which becomes prominent if you increase the padding.
On line 19, it should be: text_offset_x + text_width + 2

Good catch! Thanks :-)

@Zrufy
Copy link

Zrufy commented Dec 12, 2019

thanks for the help!

@BlasMFO
Copy link

BlasMFO commented Mar 29, 2020

Thanks, great code!!

@vinc3nt
Copy link

vinc3nt commented May 2, 2020

thanks man

@blackparadise0407
Copy link

Thanks, it helped a lot ;)

@nelsonmanohar-umich
Copy link

thanks!

@f5-bit
Copy link

f5-bit commented May 11, 2021

Thanks! I additionally included the descender (letters that extend below the baseline) like this:

(text_width, text_height), baseline = cv2.getTextSize(text, font, fontScale=font_scale, thickness=1)
...
box_coords = ((text_offset_x, text_offset_y+baseline), (text_offset_x + text_width + 2, text_offset_y - text_height - 2))

@aplz
Copy link
Author

aplz commented May 12, 2021

Thanks! I additionally included the descender (letters that extend below the baseline) like this:

(text_width, text_height), baseline = cv2.getTextSize(text, font, fontScale=font_scale, thickness=1)
...
box_coords = ((text_offset_x, text_offset_y+baseline), (text_offset_x + text_width + 2, text_offset_y - text_height - 2))

Great! 👍

@AntRocIOT
Copy link

Very usefull, thanks bro.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment