Insert Text on Images

OpenCV provides putText() function to insert text string in the image. It is interesting to note that the symbols that cannot be rendered using the specified font are replaced by question marks. The complete signature of the putText () function is given below:

putText( img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]] )

Parameter Description
img The input image.
text Text string to be drawn.
org Bottom-left corner of the text string in the image.
fontFace Font type, various types of fonts are provides in HersheyFonts.
fontScale Font scale factor that is multiplied by the font-specific base size.
color Line color.
thickness Line thickness.
lineType Type of the line, such as filled line etc.
bottomLeftOrigin When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.

Following code example shows how to insert text in an image:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import cv2
import numpy as np
import matplotlib.pyplot as plt
img1 = np.zeros((200,200,3), np.uint8)
img2 = np.zeros((200,200,3), np.uint8)
cv2.putText(img1,'Hello OpenCV!',(10,100),cv2.FONT_HERSHEY_SIMPLEX, 0.8,(255,255,255),1,cv2.LINE_AA)
cv2.putText(img2,'Hello OpenCV!',(10,100),cv2.FONT_HERSHEY_SIMPLEX, 0.8,(255,255,255),1,cv2.LINE_AA,bottomLeftOrigin=True)
fig, axs = plt.subplots(1, 2)
axs[0].imshow(img1,cmap='gray')
axs[0].axis('off')
axs[0].set_title('img1')
axs[1].imshow(img2,cmap='gray')
axs[1].axis('off')
axs[1].set_title('img2')


Line 6 and 7: The putTex() function is used to insert text on the image. In line 7 we set the bottomLeftOrigin=True. The difference of the output is shown as below. Img2 is default output and the img2 is produced when we set bottomLeftOrigin=True.

No image
Figure 1. Draw text on image using putText() method.

OpenCV Tutorials