Last modified: Jan 16, 2025 By Alexander Williams
Python OpenCV cv2.putText() Guide
Adding text to images is a common task in image processing. Python's OpenCV library provides the cv2.putText()
function for this purpose. This guide will help you understand how to use it effectively.
What is cv2.putText()?
The cv2.putText()
function is used to draw text on an image. It allows you to specify the text, position, font, scale, color, and thickness. This function is useful for annotating images with labels, timestamps, or other information.
Syntax of cv2.putText()
The syntax for cv2.putText()
is as follows:
cv2.putText(image, text, org, fontFace, fontScale, color, thickness, lineType, bottomLeftOrigin)
Here, image is the input image, text is the string to be drawn, and org is the bottom-left corner of the text. The other parameters control the appearance of the text.
Parameters Explained
Let's break down the parameters:
- image: The image where the text will be drawn.
- text: The text string to be displayed.
- org: The coordinates of the bottom-left corner of the text.
- fontFace: The font type (e.g.,
cv2.FONT_HERSHEY_SIMPLEX
). - fontScale: The font scale factor.
- color: The color of the text in BGR format.
- thickness: The thickness of the text lines.
- lineType: The type of line used (optional).
- bottomLeftOrigin: If true, the origin is at the bottom-left (optional).
Example: Adding Text to an Image
Let's see an example of how to use cv2.putText()
to add text to an image.
import cv2
import numpy as np
# Create a black image
image = np.zeros((500, 500, 3), dtype=np.uint8)
# Add text to the image
text = "Hello, OpenCV!"
position = (50, 250)
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 1
color = (255, 255, 255)
thickness = 2
cv2.putText(image, text, position, font, font_scale, color, thickness)
# Display the image
cv2.imshow("Text on Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
In this example, we create a black image and add the text "Hello, OpenCV!" at the position (50, 250). The text is white, with a font scale of 1 and a thickness of 2.
Output
The output will display a black image with the text "Hello, OpenCV!" written in white at the specified position.
# Output: A window displaying the image with the text "Hello, OpenCV!"
Common Use Cases
cv2.putText()
is often used in applications like:
- Annotating images with labels or timestamps.
- Displaying results of image processing tasks.
- Creating visualizations for machine learning models.
For example, you can use it in conjunction with cv2.rectangle() to draw bounding boxes and label objects in an image.
Tips for Using cv2.putText()
Here are some tips to get the most out of cv2.putText()
:
- Choose a font that is easy to read.
- Adjust the font scale and thickness for better visibility.
- Use contrasting colors for the text and background.
If you're working with edge detection, you might also find cv2.Canny() useful for preprocessing images before adding text.
Conclusion
The cv2.putText()
function is a powerful tool for adding text to images in Python. It is easy to use and highly customizable. Whether you're annotating images or creating visualizations, this function will help you achieve your goals.
For more advanced image processing techniques, check out our guides on cv2.line() and cv2.circle().