Home > Back-end >  Opencv print text with outline
Opencv print text with outline

Time:11-19

I would like to render a custom text on an image using opencv, while having a white outline around the text, with the inside of the text being black. Currently I am only able to print the text in a single static color (black in this case) like so:

cv2.putText(img, "My text", (x, y), font, font_size, (0, 0, 0), font_thickness, lineType = cv2.LINE_AA)

How can I achieve an effect like the one shown below, where the outline of the text is printed in white, and the inner part in black?

enter image description here

Note: The text is placed on an image, so the remaining space (the grey space in the example image) around the text should be transparent.

CodePudding user response:

You could draw the text twice on the image: one for your outline and the other for your text.

cv2.putText(image,"text",(180,150),cv2.FONT_HERSHEY_COMPLEX,3,(255,255,255),16,cv2.LINE_AA)
cv2.putText(image,"text",(180,150),cv2.FONT_HERSHEY_COMPLEX,3,(0,0,0),4,cv2.LINE_AA)

enter image description here

  • Related