Skip to content

Instantly share code, notes, and snippets.

@evilmtv
Forked from xcsrz/center_text_on_image.py
Last active December 27, 2021 11:51
Show Gist options
  • Save evilmtv/af2a023e472e6303fd2d3cc02aa1a83a to your computer and use it in GitHub Desktop.
Save evilmtv/af2a023e472e6303fd2d3cc02aa1a83a to your computer and use it in GitHub Desktop.
Center text on an image with Python and OpenCV.
# setup text
font = cv2.FONT_HERSHEY_SIMPLEX
text = "Female"
# get boundary of this text
textsize = cv2.getTextSize(text, font, 1, 2)[0]
# get coords based on boundary
textX = int(boundingboxcenter[0] - (textsize[0] / 2))
textY = int(boundingboxcenter[1] + (textsize[1] / 2))
# add text centered on image
cv2.putText(img, text, (textX, textY ), font, 1, (255, 255, 255), 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment