OpenCV putText() new line character

cv2.putText 在向图像中添加文本信息时,如果在待添加的文本中含有换行转义符,一般它是无法正确处理的:

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

一种解决方案如下:

img = cv2.imread('boat.png')
text = 'This is \nsome text'
y0, dy = 50, 25

for i, txt in enumerate(text.split('\n')):
    y = y0+i*dy
    cv2.putText(img, txt, (50, y), cv2.FONT_HERSHEY_SIMPLEX,  .6, (0, 255, 0), 1, 2)

cv2.imshow('img', img)
cv2.waitKey(0)
posted on 2017-05-05 19:09  未雨愁眸  阅读(147)  评论(0编辑  收藏  举报