If you are solving a computer vision problem then you must know about OpenCV. It is a library that allows you to manipulate images. For this, it uses many inbuilt functions. The cv2 putText() is one of them. In this entire tutorial, you will learn how to implement the cv2 putText() method in python.
Steps to implement cv2 putText()
In this section, you have to follow the given steps for better understanding. Before implementing please check whether OpenCV is installed or not in your system. If it’s not then know how to install OpenCV using pip.
Step 1: Import Required Libraries
The first and most important step is to import the necessary library. In this entire tutorial, I am using only OpenCv so lets import it using the import statement.
Step 2 : Read the image
The second step before writing the text on the image is to read the image. In OpenCV there is a function cv.imread() that allows you to read the image. Let’s read the image using it.
img = cv2.imread("bird.jpg")
Below is the image I am reading.
Step 3: Implement the following examples
Now lets implement the cv2.putText() function to write the text on the image. But before that lets know the syntax for this method.
cv2.putText(image, text, org, font, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])
The following is the explanation of all the parameters used.
text: Text string to be drawn.
org: Bottom-left corner of the text string in the image.
fontFace: Font type, HersheyFonts.
fontScale: Font scale factor that is multiplied by the font-specific base size.
color: Text color.
thickness: Thickness of the lines used to draw a text.
lineType: Line type.
Example 1 : Writing simple text to the image
Lets write simple text to the image I have read in step 2. In this example I will write “Data Science Learner” to the image . Execute the below lines of code.
import cv2 img = cv2.imread("bird.jpg") font = cv2.FONT_HERSHEY_SIMPLEX org = (250, 250) fontScale = 1 fontFace = 3 color = (255, 255, 255) thickness = 2 text = "Data Science Learner" image = cv2.putText(img,text,org,fontFace,fontScale,color) cv2.imshow("Bird Image",image) cv2.waitKey(0)
You will get the following output when you will run the above code.
Example 2: Adding thickness to the text
In the second example I will add thickeness to the text. To do so you have to pass thickness argument inside the cv2.putText() method. Just execute the following lines of code and see the output.
import cv2 img = cv2.imread("bird.jpg") font = cv2.FONT_HERSHEY_SIMPLEX org = (250, 250) fontScale = 1 fontFace = 3 color = (255, 255, 255) thickness = 3 text = "Data Science Learner" image = cv2.putText(img,text,org,fontFace,fontScale,color,thickness) cv2.imshow("Bird Image",image) cv2.waitKey(0)
These are the steps to implement cv2 putText() function in python. There are much application of this method and the common is writing names of person on the image after face detection. You can use this method to innovate your products. I hope you have liked this tutorial. If you have any suggestions then you can contact us for more help.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.