OpenCV putText()换行符 [英] OpenCV putText() new line character

查看:617
本文介绍了OpenCV putText()换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用cv2.putText()在图像上绘制文本字符串.

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

当我写:

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

图像上绘制的文字是:

This is ? some text

我希望文本会在新行中打印,因为\n是换行符的转义字符,但它会在图像上绘制?.

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 ?

推荐答案

不幸的是,putText无法正确处理\n符号.请参阅相关的拒绝拉取请求.您需要自己分割文本并进行几次putText调用,例如:

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)

这篇关于OpenCV putText()换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆