How to write text on a image in windows using python opencv2

238,838

Solution 1

This code uses cv2.putText to overlay text on an image. You need NumPy and OpenCV installed.

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)
thickness              = 1
lineType               = 2

cv2.putText(img,'Hello World!', 
    bottomLeftCornerOfText, 
    font, 
    fontScale,
    fontColor,
    thickness,
    lineType)

#Display the image
cv2.imshow("img",img)

#Save image
cv2.imwrite("out.jpg", img)

cv2.waitKey(0)

Solution 2

Was CV_FONT_HERSHEY_SIMPLEX in cv(1)? Here's all I have available for 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.

cv2.putText(image,"Hello World!!!", (x,y), cv2.FONT_HERSHEY_SIMPLEX, 2, 255)

Solution 3

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.

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

Solution 4

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

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).

Solution 5

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

    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

Share:
238,838
Chandra Shaker Balure
Author by

Chandra Shaker Balure

Updated on July 08, 2022

Comments

  • Chandra Shaker Balure
    Chandra Shaker Balure almost 2 years

    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'

    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.

    Any suggestions?

  • João Cartucho
    João Cartucho about 6 years
    It may be useful to know the size of the written text: text_width, text_height = cv2.getTextSize(text_description, font, fontScale, lineType)[0]
  • Craig S. Anderson
    Craig S. Anderson about 6 years
    How did you get the list of available fonts?
  • Peter Hansen
    Peter Hansen over 5 years
    @CraigS.Anderson My approach for that is usually just at the Python prompt, like this: import cv2; [x for x in dir(cv2) if x.startswith('FONT_')]
  • Alan Turing
    Alan Turing over 5 years
    Here lineType should be lineThickness since that is the parameter list. See: docs.opencv.org/2.4/modules/core/doc/…
  • Joseph Budin
    Joseph Budin about 5 years
    Although your solution apparently works, I think it does not answer the question which specifies it uses Opencv.
  • bunkus
    bunkus about 5 years
    You can use the library PILasOPENCV github.com/bunkahle/PILasOPENCV which is a wrapper around common PIL functions but internally only uses OPENCV
  • CS QGB
    CS QGB over 2 years
    In [471]: [(i,getattr(cv2,i),) for i in dir(cv2) if i.startswith('FONT_')] Out[471]: [('FONT_HERSHEY_COMPLEX', 3), ('FONT_HERSHEY_COMPLEX_SMALL', 5), ('FONT_HERSHEY_DUPLEX', 2), ('FONT_HERSHEY_PLAIN', 1), ('FONT_HERSHEY_SCRIPT_COMPLEX', 7), ('FONT_HERSHEY_SCRIPT_SIMPLEX', 6), ('FONT_HERSHEY_SIMPLEX', 0), ('FONT_HERSHEY_TRIPLEX', 4), ('FONT_ITALIC', 16)]