如何使用python opencv2在Windows中的图像上写文本

时间:2021-09-01 23:30:26

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'

它给出了ERROR,说'module'对象没有属性'CV_FONT_HERSHEY_SIMPLEX'

Query Can'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 putText to pass the font type as parameter. But it is not working for me.

查询我不能使用上面的字体类型?我在互联网上搜索,但发现只有与OpenFv C ++ for initFont相关的语法。然后我想到使用putText将字体类型作为参数传递。但它对我不起作用。

Any suggestions?

6 个解决方案

#1


30  

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)

#2


22  

Was CV_FONT_HERSHEY_SIMPLEX in cv(1)? Here's all I have available for cv2 "FONT":

cv(1)中有CV_FONT_HERSHEY_SIMPLEX吗?以下是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)

#3


7  

This is indeed a bit of an annoying problem. For python 2.x.x you use:

这确实是一个恼人的问题。对于python 2.x.x,您使用:

cv2.CV_FONT_HERSHEY_SIMPLEX

and for Python 3.x.x:

对于Python 3.x.x:

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(如果它们不确保您更改代码)。

#4


1  

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:

这确实是一个恼人的问题。对于python 2.x.x,您使用:

cv2.CV_FONT_HERSHEY_SIMPLEX and for Python 3.x.x:

cv2.CV_FONT_HERSHEY_SIMPLEX和Python 3.x.x:

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

#5


0  

I had a similar problem. I would suggest using the PIL library 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.

我有类似的问题。我建议在python中使用PIL库,因为它绘制任何给定字体的文本,与OpenCV中的有限字体相比。使用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

如何使用python opencv2在Windows中的图像上写文本

#6


0  

The underlying principles are the same. But before you add the actual text from a quick glance at the FONT that OpenCV provides you with. These fonts are defined through an enumeration.

基本原则是相同的。但是,在您快速浏览OpenCV为您提供的FONT添加实际文本之前。这些字体是通过枚举定义的。

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,

Take one of these, for example the first one and add it to the code:

取其中一个,例如第一个并将其添加到代码中:

font = cv.FONT_HERSHEY_SIMPLEX

Then to draw the text with OpenCV there is cv2.putText function that accepts a number of arguments:

然后使用OpenCV绘制文本,有cv2.putText函数接受许多参数:

the image on which to draw the text to be written coordinates of the text start point font to be used font size text color text thickness the type of line used

要在其上绘制文本的图像要写入的文本起始点字体的坐标要使用的字体大小文本颜色文本粗细使用的行的类型

So if you want to add a text to the logos.jpg image, write the following line, adding it to the code.

因此,如果要将文本添加到logos.jpg图像,请编写以下行,将其添加到代码中。

cv.putText(img, 'This one!", (230, 50), font, 0.8, (0, 255, 0), 2, cv.LINE_AA)

#1


30  

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)

#2


22  

Was CV_FONT_HERSHEY_SIMPLEX in cv(1)? Here's all I have available for cv2 "FONT":

cv(1)中有CV_FONT_HERSHEY_SIMPLEX吗?以下是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)

#3


7  

This is indeed a bit of an annoying problem. For python 2.x.x you use:

这确实是一个恼人的问题。对于python 2.x.x,您使用:

cv2.CV_FONT_HERSHEY_SIMPLEX

and for Python 3.x.x:

对于Python 3.x.x:

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(如果它们不确保您更改代码)。

#4


1  

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:

这确实是一个恼人的问题。对于python 2.x.x,您使用:

cv2.CV_FONT_HERSHEY_SIMPLEX and for Python 3.x.x:

cv2.CV_FONT_HERSHEY_SIMPLEX和Python 3.x.x:

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

#5


0  

I had a similar problem. I would suggest using the PIL library 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.

我有类似的问题。我建议在python中使用PIL库,因为它绘制任何给定字体的文本,与OpenCV中的有限字体相比。使用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

如何使用python opencv2在Windows中的图像上写文本

#6


0  

The underlying principles are the same. But before you add the actual text from a quick glance at the FONT that OpenCV provides you with. These fonts are defined through an enumeration.

基本原则是相同的。但是,在您快速浏览OpenCV为您提供的FONT添加实际文本之前。这些字体是通过枚举定义的。

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,

Take one of these, for example the first one and add it to the code:

取其中一个,例如第一个并将其添加到代码中:

font = cv.FONT_HERSHEY_SIMPLEX

Then to draw the text with OpenCV there is cv2.putText function that accepts a number of arguments:

然后使用OpenCV绘制文本,有cv2.putText函数接受许多参数:

the image on which to draw the text to be written coordinates of the text start point font to be used font size text color text thickness the type of line used

要在其上绘制文本的图像要写入的文本起始点字体的坐标要使用的字体大小文本颜色文本粗细使用的行的类型

So if you want to add a text to the logos.jpg image, write the following line, adding it to the code.

因此,如果要将文本添加到logos.jpg图像,请编写以下行,将其添加到代码中。

cv.putText(img, 'This one!", (230, 50), font, 0.8, (0, 255, 0), 2, cv.LINE_AA)