Skip to content

Instantly share code, notes, and snippets.

@anujonthemove
Created June 16, 2020 14:10
Show Gist options
  • Save anujonthemove/2ca68c5f321e2efd5b83be45b00248af to your computer and use it in GitHub Desktop.
Save anujonthemove/2ca68c5f321e2efd5b83be45b00248af to your computer and use it in GitHub Desktop.
def text_over_rectangle(coords, text, image, color):
x, y = coords
# font
fontFace = cv2.FONT_HERSHEY_SIMPLEX
# org
org = (20, 50)
# fontScale
fontScale = 2
# Line thickness of 2 px
thickness = 3
# Using cv2.putText() method
fontColor = (255, 255, 255)
(text_width, text_height) = cv2.getTextSize(text.capitalize(), fontFace, fontScale, thickness)[0]
bg_coords = ((int(x), int(y)-text_height-30), (int(x)+text_width+10, int(y)))
cv2.rectangle(image, bg_coords[0], bg_coords[1], color, cv2.FILLED)
cv2.putText(image, text.capitalize(), (int(x), int(y-20)), fontFace, fontScale, fontColor, thickness)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment