Answer a question

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 ?

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)
Logo

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐