cv2.putText 文字换行('\n')无法解析换行

来源:互联网 发布:四川麻将血战定缺算法 编辑:程序博客网 时间:2024/06/04 18:22

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, 25for 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)
0 0
原创粉丝点击