Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV putText() new line character

Tags:

python

opencv

I am using cv2.putText() to draw a text string on an image.

When I write:

cv2.putText(img, "This is \n some text", (50,50), cv2.FONT_HERSHEY_SIMPLEX, 1, 2) 

The text drawn on the image is:

This is ? some text

I was expecting the text to be printed in the new line as \n is an escape character for newline but it draws ? on the image instead.

Why this is happening ? am I doing something wrong ?

like image 725
Vipul Avatar asked Dec 25 '14 13:12

Vipul


1 Answers

Unfortunately putText doesn't correctly handle \n symbols. See the relevant rejected pull request. You need to split your text yourself and make several putText calls, something like:

text = "This is \n some text" y0, dy = 50, 4 for i, line in enumerate(text.split('\n')):     y = y0 + i*dy     cv2.putText(img, line, (50, y ), cv2.FONT_HERSHEY_SIMPLEX, 1, 2) 
like image 183
elyase Avatar answered Oct 12 '22 01:10

elyase