如何使用python opencv2在Windows中的图像上写入文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16615662/
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
How to write text on a image in windows using python opencv2
提问by Chandra Shaker Balure
I want to put some text on an Image. I am writing the code as:
我想在图像上放一些文字。我将代码编写为:
cv2.putText(image,"Hello World!!!", (x,y), cv2.CV_FONT_HERSHEY_SIMPLEX, 2, 255)
It gives ERROR, saying 'module' object has no attribute 'CV_FONT_HERSHEY_SIMPLEX'
它给出了错误,说“模块”对象没有属性“CV_FONT_HERSHEY_SIMPLEX”
QueryCan't I use the font type as above? I searched in internet, but found only the syntax related to to Opencv C++ for initFont.
Then I thought of using putTextto pass the font type as parameter.
But it is not working for me.
查询我不能使用上述字体类型吗?我在互联网上搜索,但只找到了与 initFont 的 Opencv C++ 相关的语法。然后我想到使用putText将字体类型作为参数传递。但它对我不起作用。
Any suggestions?
有什么建议?
回答by Andenthal
Was CV_FONT_HERSHEY_SIMPLEXin cv(1)?
Here's all I have available for cv2 "FONT":
是否CV_FONT_HERSHEY_SIMPLEX在 cv(1) 中?这是我可用于 cv2 "FONT" 的所有内容:
FONT_HERSHEY_COMPLEX
FONT_HERSHEY_COMPLEX_SMALL
FONT_HERSHEY_DUPLEX
FONT_HERSHEY_PLAIN
FONT_HERSHEY_SCRIPT_COMPLEX
FONT_HERSHEY_SCRIPT_SIMPLEX
FONT_HERSHEY_SIMPLEX
FONT_HERSHEY_TRIPLEX
FONT_ITALIC
Dropping the 'CV_' seems to work for me.
删除“CV_”似乎对我有用。
cv2.putText(image,"Hello World!!!", (x,y), cv2.FONT_HERSHEY_SIMPLEX, 2, 255)
回答by Vinay Vemula
This code uses cv2.putText to overlay text on an image. You need NumPy and OpenCV installed.
此代码使用 cv2.putText 在图像上覆盖文本。您需要安装 NumPy 和 OpenCV。
import numpy as np
import cv2
# Create a black image
img = np.zeros((512,512,3), np.uint8)
# Write some Text
font = cv2.FONT_HERSHEY_SIMPLEX
bottomLeftCornerOfText = (10,500)
fontScale = 1
fontColor = (255,255,255)
lineType = 2
cv2.putText(img,'Hello World!',
bottomLeftCornerOfText,
font,
fontScale,
fontColor,
lineType)
#Display the image
cv2.imshow("img",img)
#Save image
cv2.imwrite("out.jpg", img)
cv2.waitKey(0)
回答by Roeffus
This is indeed a bit of an annoying problem. For python 2.x.x you use:
这确实是一个有点烦人的问题。对于 python 2.xx,您使用:
cv2.CV_FONT_HERSHEY_SIMPLEX
and for Python 3.x.x:
对于 Python 3.xx:
cv2.FONT_HERSHEY_SIMPLEX
I recommend using a autocomplete environment(pyscripter or scipy for example). If you lookup example code, make sure they use the same version of Python(if they don't make sure you change the code).
我建议使用自动完成环境(例如 pyscripter 或 scipy)。如果您查找示例代码,请确保它们使用相同版本的 Python(如果它们不确保您更改代码)。
回答by Ravinder Payal
Here's the code with parameter labels
这是带有参数标签的代码
def draw_text(self, frame, text, x, y, color=BGR_COMMON['green'], thickness=1.3, size=0.3,):
if x is not None and y is not None:
cv2.putText(
frame, text, (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, size, color, thickness)
For font name please see another answer in this thread.
对于字体名称,请参阅此线程中的另一个答案。
Excerpt from answer by @Roeffus
摘自@Roeffus 的回答
This is indeed a bit of an annoying problem. For python 2.x.x you use:
cv2.CV_FONT_HERSHEY_SIMPLEX and for Python 3.x.x:
cv2.FONT_HERSHEY_SIMPLEX
这确实是一个有点烦人的问题。对于 python 2.xx,您使用:
cv2.CV_FONT_HERSHEY_SIMPLEX 和 Python 3.xx:
cv2.FONT_HERSHEY_SIMPLEX
For more see this http://www.programcreek.com/python/example/83399/cv2.putText
有关更多信息,请参见http://www.programcreek.com/python/example/83399/cv2.putText
回答by DollarAkshay
I had a similar problem. I would suggest using the PILlibrary in python as it draws the text in any given font, compared to limited fonts in OpenCV. With PIL you can choose any font installed on your system.
我有一个类似的问题。PIL与 OpenCV 中的有限字体相比,我建议在 python 中使用该库,因为它以任何给定的字体绘制文本。使用 PIL,您可以选择系统上安装的任何字体。
from PIL import ImageFont, ImageDraw, Image
import numpy as np
import cv2
image = cv2.imread("lena.png")
# Convert to PIL Image
cv2_im_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
pil_im = Image.fromarray(cv2_im_rgb)
draw = ImageDraw.Draw(pil_im)
# Choose a font
font = ImageFont.truetype("Roboto-Regular.ttf", 50)
# Draw the text
draw.text((0, 0), "Your Text Here", font=font)
# Save the image
cv2_im_processed = cv2.cvtColor(np.array(pil_im), cv2.COLOR_RGB2BGR)
cv2.imwrite("result.png", cv2_im_processed)
result.png
结果.png


回答by bunkus
for the example above the solution would look like this:
对于上面的示例,解决方案如下所示:
import PILasOPENCV as Image
import PILasOPENCV as ImageDraw
import PILasOPENCV as ImageFont
# from PIL import ImageFont, ImageDraw, Image
import numpy as np
import cv2
image = cv2.imread("lena.jpg")
# Convert to PIL Image
cv2_im_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
pil_im = Image.fromarray(cv2_im_rgb)
draw = ImageDraw.Draw(pil_im)
# Choose a font
font = ImageFont.truetype("Roboto-Regular.ttf", 40)
# Draw the text
draw.text((0, 0), "Your Text Here", font=font)
# Save the image
cv2_im_processed = pil_im.getim()
cv2.imshow("cv2_im_processed", cv2_im_processed)
cv2.waitKey()
回答by gerard Garvey
I know this is a really old question but i think i have a solution In the newer versions of openCV fonts are repesented by a number like this
我知道这是一个非常老的问题,但我想我有一个解决方案 在较新版本的 openCV 字体中,用这样的数字表示
FONT_HERSHEY_SIMPLEX = 0,
FONT_HERSHEY_PLAIN = 1,
FONT_HERSHEY_DUPLEX = 2,
FONT_HERSHEY_COMPLEX = 3,
FONT_HERSHEY_TRIPLEX = 4,
FONT_HERSHEY_COMPLEX_SMALL = 5,
FONT_HERSHEY_SCRIPT_SIMPLEX = 6,
FONT_HERSHEY_SCRIPT_COMPLEX = 7,
FONT_ITALIC = 16
so all you have to do is replace the font name with the corresponding number
所以你所要做的就是用相应的数字替换字体名称
cv2.putText(image,"Hello World!!!", (x,y), 0, 2, 255)
again i know its an old question but it may help someone in the future
我再次知道这是一个老问题,但它可能会在未来帮助某人
回答by Ajai
You can use cv2.FONT_HERSHEY_SIMPLEX instead of cv2.CV_FONT_HERSHEY_SIMPLEX in current opencv version
您可以在当前 opencv 版本中使用 cv2.FONT_HERSHEY_SIMPLEX 而不是 cv2.CV_FONT_HERSHEY_SIMPLEX

