Python OpenCV putText() 换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27647424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:03:40  来源:igfitidea点击:

OpenCV putText() new line character

pythonopencv

提问by Vipul

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

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

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

This is ? some text

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

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

Why this is happening ? am I doing something wrong ?

为什么会这样?难道我做错了什么 ?

采纳答案by elyase

Unfortunately putTextdoesn't correctly handle \nsymbols. See the relevant rejected pull request. You need to split your text yourself and make several putTextcalls, something like:

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

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)