The cv2.putText() function has the following signature:
img = cv.putText( img, text, org, fontFace, fontScale, color, thickness=1, lineType= 8, bottomLeftOrigin=False)
This function draws the provided text string starting at the org coordinate (upper-left corner if bottomLeftOrigin = False and lower-left corner otherwise) using the font type provided by fontFace and the fontScale factor. In connection with this example, you can see that the last provided parameter, which is lineType, takes the three different values available in OpenCV (cv2.LINE_4, cv2.LINE_8, and cv2.LINE_AA). In this way, you can see the difference better when plotting these types. Remember that cv2.LINE_AA gives much better quality (an anti-aliased line type), but it is slower to draw than the other two types. The key code to draw some text is given next. The full code for this example can be seen in the text_drawing.py script:
# We draw some text on the image:
cv2.putText(image, 'Mastering OpenCV4 with Python', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.9, colors['red'], 2, cv2.LINE_4)
cv2.putText(image, 'Mastering OpenCV4 with Python', (10, 70), cv2.FONT_HERSHEY_SIMPLEX, 0.9, colors['red'], 2, cv2.LINE_8)
cv2.putText(image, 'Mastering OpenCV4 with Python', (10, 110), cv2.FONT_HERSHEY_SIMPLEX, 0.9, colors['red'], 2, cv2.LINE_AA)
# Show image:
show_with_matplotlib(image, 'cv2.putText()')
In the next screenshot, you can see the result:
In this example, the background color is set to white. To perform this functionality, you can do the following:
image.fill(255)