Hello fellow learner! In this tutorial, we will learn how to write string text on Images in Python using the OpenCV putText() method. So let’s get started.
What is the OpenCV putText() method?
putText() method which is used to put text on any image. The method uses following parameters.
- img: The Image on which you want to write the text.
- text: The text you want to write on the image.
- org: It is the coordinates of the Bottom-Left corner of your text. It is represented as a tuple of 2 values (X, Y). X represents the distance from the left edge and Y represents the distance from the top edge of the image.
- fontFace: It denotes the type of font you want to use. OpenCV supports only a subset of Hershey Fonts.
- fontScale: It is used to increase/decrease the size of your text. The font scale factor is multiplied by the font-specific base size.
- color: It represents the color of the text that you want to give. It takes the value in
BGRformat, i.e., first blue color value, then green color value, and the red color value all in range 0 to 255.
- thickness (Optional): It represents the thickness of the lines used to draw a text. The default value is 1.
- lineType (Optional): It denotes the type of line you want to use. 4 LineTypes available are
- LINE_8 (Default)
- bottomLeftOrigin (Optional): When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner. The default value is False.
Adding text on an image using OpenCV – cv2.putText() method
Lets use the below image to write a “Good Morning” message using OpenCV putText() method.
# importing cv2 library import cv2 # Reading the image image = cv2.imread("Wallpaper.jpg") # Using cv2.putText() new_image = cv2.putText( img = image, text = "Good Morning", org = (200, 200), fontFace = cv2.FONT_HERSHEY_DUPLEX, fontScale = 3.0, color = (125, 246, 55), thickness = 3 ) # Saving the new image cv2.imwrite("New Wallpaper.jpg", new_image)
In this tutorial, you learned about how to use OpenCV putText() method to write text on image. Thanks for reading!!