Updated April 6, 2023
Introduction to OpenCV putText()
OpenCV putText() is a command present in the OpenCV library, Which is used in order for putting a specified text within the image that the user has provided by utilizing the put text function. This function has been seen to be defined within the img proc.hpp header in a python programming language. To put text within the specified image as per the user’s requirement, firstly, a declaration has to be made for a matrix that executes the function of loading the required image.
It is also needed that the starting point for the text has to be defined within the matrix. there is also a requirement for defining the font color for the text, the font style for the text, and the weight that the text has two be specified with. The function is present in the OpenCV library of Python programming language, which is a one-stop solution that has been designed in order to solve problems related to computer vision.
Syntax for OpenCV putText()
Following is the syntax which is used for implementation of the OpenCV put text function:
cv2.put Text (* image *, text *, org *, font *, fontScale *, color [*, thickness [,line Type [*,* bottom Left Origin *] *] *] *) *
Parameters for OpenCV put text function
The following parameters are accepted by the OpenCV putText() function:
Parameters | Description of the parameters: |
image | this parameter represents the original image that the user has selected to add text by the system |
text | This parameter represents the text that has to be drawn by the system as specified by the user |
org | This parameter is used to represent the coordinate with respect to the text present on the bottom left corner of the image. These coordinates are represented with the help of 2 double values that are passed, which represent the X coordinate and the Y coordinate values, respectively |
font | This parameter is used to represent the type or style of font, which would be denoted for the string text that the user specifies. some instances for the kinds of font types that can be used are FONT * _ * HERSHEY * _ * PLAIN OR FONT * _ * HERSHEY * _ * SIMPLEX |
font * scale | This parameter represents the best size for the specified font size, which is relevant to the font scale factor, which acts as a multiplying factor further font size of the text that has to be entered |
thickness | this parameter represents the thickness that has to be given for the line of text that has to be entered by the user. it is measured in terms of pixel size |
color | this parameter represents the specific color that has to be given to the text string that is being entered into the image that is being drawn on the screen. The color is extracted from the BGR tuple, which is passed to it. for instance, for a text of blue color the tuple to be passed would be * (* 255 *, * 0 *, * 0) * |
Line * Type | This parameter is used to define the type of line used for the text, which has to be entered into the image. This parameter is an optional parameter. |
Bottom * Left * Origin | this parameter is used for defining the position for the image data origin with respect to the directional position in the image. this parameter is an optional parameter. If the parameter is taken as true, the image data origin is found to be placed at the bottom left corner of the image. If it is not true, the image data origin is placed at the top left corner of the image. |
Return * Value | This method is responsible for returning an output image that is to be loaded from the file which has been specified. |
Example of OpenCV putText()
Following is the example which is used in order to demonstrate how the OpenCV putText() command is utilized in the Python programming language
# command used to import the OpenCV library to utilize OpenCV read image function
import cv2
# path being defined from where the system will read the image
path = r'C:\Users \ Priyanka \ Desktop \ educba \ OpenCV \ edu cba logo.png'
# command used for reading an image from the disk disk, cv2.imread function is used
image1 = cv2.imread(path)
# Window name being specified where the image will be displayed
window_name1 = 'image'
# font for the text being specified
font1 = cv2.FONT_HERSHEY_SIMPLEX
# org for the text being specified
org1 = (50, 50)
# font scale for the text being specified
fontScale1 = 1
# Blue color for the text being specified from BGR
color1 = ( 255 , 0 , 0 )
# Line thickness for the text being specified at 2 px
thickness1 = 2
# Using the cv2.putText() method for inserting text in the image of the specified path
image_1 = cv2.putText(image1, 'EDU CBA', org1, font1,
fontScale1, color1, thickness1, cv2.LINE_AA)
# Displaying the output image
cv2.imshow(window_name, image_1)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
Conclusion
The OpenCV putText() method is a very useful function present in the OpenCV library, which allows the system to add text to an image that the user has provided. There are several image processing areas where text needs to be associated with the images that are being processed, and there needs to be a variety in the color, font style, width, and orientation in terms of the position whether the text has to be placed on the image which can easily be utilized by using the put text method. It also reduces the verbosity of the program that is being written and increases the overall processing speed for the program to be executed.
Recommended Articles
We hope that this EDUCBA information on “OpenCV putText” was beneficial to you. You can view EDUCBA’s recommended articles for more information.