Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I have an image & over that image, I want to write some text. Look at the following code,

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

When I write code like this, It gives an ERROR, saying 

'module' object has no attribute 'CV_FONT_HERSHEY_SIMPLEX'

My Doubt was, Is it possible to use font type as above? When I surfed on the internet, I could able to find only the syntax that is related to OpenCV C++ for initFont. Later, I thought of using putText for passing the font type as a parameter. But still, it's not working for me? Any suggestions?

1 Answer

0 votes
by (26.4k points)

The following code uses cv2.putText to overlay text over an image. For that, make sure you have installed Numpy and OpenCV in your system.

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)

Are you interested to learn more  topics related to Python? Come and Join: Python training course

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...