Updated April 19, 2023
Introduction to OpenCV text
Whenever there is a necessity to write text on an image, we make use of a function called putText() function in OpenCV which is very much essential in solving computer vision problems and the parameters to be passed to this putText() function are the image on which the text is supposed to be written, the text that is supposed to be written on the image, the position of the text on the image along the horizontal and vertical axes, the font of the text, the font size of the text, the font color of the text, the thickness of the font line in the text and this putText() function returns an image with text written on it.
The syntax to define putText() function in OpenCV is as follows:
cv2.putText(image, text, position, textfont, fontsize, fontcolor, thickness)
where image is the image on which the text is supposed to be written,
text is the text that is supposed to be written on the image,
position is the position of the text on the image along the horizontal and vertical axes,
textfont is the font of the text,
fontsize is the font size of the text,
fontcolor is the font color of the text,
thickness is the thickness of the font line in the text.
Working of putText() function in OpenCV
- Whenever there is a necessity to write text on an image, we make use of a function called the putText() function in OpenCV.
- The putText() function in OpenCV is very much essential in solving computer vision problems.
- Some of the font types of the text to be written on the image are FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, etc.
- The putText() function returns an image with text written on it.
Examples of OpenCV text
Here are the following examples mention below
Example #1
OpenCV program in python to demonstrate putText() function using which we are going to write a text on the image and display the image with text written on it as the output on the screen:
Code:
#importing the modules opencv
import cv2
#reading the image on which the text is to be written using imread() function
Imagewrite = cv2.imread('C:/Users/admin/Desktop/logo.png', cv2.IMREAD_UNCHANGED)
#storing the position of the text on the image along the horizontal and vertical axes in a variable
textposition = (4, 9)
#using putText() function to write the text on the image
cv2.putText(
#specifying the image on which text must be written
imagewrite,
#specifying the text to be written on the image
"Welcome to EDUCBA",
#specifying the position of the text on the image
textposition,
#specifying the font text of the image
cv2.FONT_HERSHEY_SIMPLEX,
#specifying the font size of the text
0.4,
#specifying the font color of the text
(209, 80, 0, 255),
#specifying the thickness of the font line in the text
2)
#displaying the resulting image as the output on the screen
cv2.imwrite("C:/Users/admin/Desktop/logo1.png", imagewrite)
Output:
In the above program, we are importing the module cv2. Then we are reading the image on which the text is supposed to be written using the imread() function from the cv2 module. Then we are storing the position of the text on the image along the horizontal and vertical axes in a variable. Then we are using the putText() function to write the text on the image by specifying all the parameters. Then we are displaying the resulting image with text written on it as the output on the screen. The output is shown in the snapshot above.
Example #2
OpenCV program in python to demonstrate putText() function using which we are going to write a text on the image and display the image with text written on it as the output on the screen:
Code:
#importing the modules opencv
import cv2
#reading the image on which the text is to be written using imread() function
Imagewrite = cv2.imread('C:/Users/admin/Desktop/plane.jpg', cv2.IMREAD_UNCHANGED)
#storing the position of the text on the image along the horizontal and vertical axes in a variable
textposition = (400, 100)
#using putText() function to write the text on the image
cv2.putText(
#specifying the image on which text must be written
imagewrite,
#specifying the text to be written on the image
"Welcome to EDUCBA",
#specifying the position of the text on the image
textposition,
#specifying the font text of the image
cv2.FONT_HERSHEY_SIMPLEX,
#specifying the font size of the text
1,
#specifying the font color of the text
(0, 0, 0, 255),
#specifying the thickness of the font line in the text
3)
#displaying the resulting image as the output on the screen
cv2.imwrite("C:/Users/admin/Desktop/logo1.png", imagewrite)
Output:
In the above program, we are importing the module cv2. Then we are reading the image on which the text is supposed to be written using the imread() function from the cv2 module. Then we are storing the position of the text on the image along the horizontal and vertical axes in a variable. Then we are using the putText() function to write the text on the image by specifying all the parameters. Then we are displaying the resulting image with text written on it as the output on the screen. The output is shown in the snapshot above.
Example #3
OpenCV program in python to demonstrate putText() function using which we are going to write a text on the image and display the image with text written on it as the output on the screen:
Code:
#importing the modules opencv
import cv2
#reading the image on which the text is to be written using imread() function
Imagewrite = cv2.imread('C:/Users/admin/Desktop/tree.jpg', cv2.IMREAD_UNCHANGED)
#storing the position of the text on the image along the horizontal and vertical axes in a variable
textposition = (60, 150)
#using putText() function to write the text on the image
cv2.putText(
#specifying the image on which text must be written
imagewrite,
#specifying the text to be written on the image
"Welcome to EDUCBA",
#specifying the position of the text on the image
textposition,
#specifying the font text of the image
cv2.FONT_HERSHEY_SIMPLEX,
#specifying the font size of the text
0.4,
#specifying the font color of the text
(0, 0, 0, 255),
#specifying the thickness of the font line in the text
2)
#displaying the resulting image as the output on the screen
cv2.imwrite("C:/Users/admin/Desktop/logo1.png", imagewrite)
Output:
In the above program, we are importing the module cv2. Then we are reading the image on which the text is supposed to be written using the imread() function from the cv2 module. Then we are storing the position of the text on the image along the horizontal and vertical axes in a variable. Then we are using the putText() function to write the text on the image by specifying all the parameters. Then we are displaying the resulting image with text written on it as the output on the screen. The output is shown in the snapshot above.
Recommended Articles
We hope that this EDUCBA information on “OpenCV text” was beneficial to you. You can view EDUCBA’s recommended articles for more information.