OpenCV Tutorial
Image Processing
Feature Detection and Description
Drawing Functions
Video Processing
Applications and Projects
Drawing text on an image can be useful in many scenarios like watermarking or labeling. OpenCV provides the putText()
function to overlay text on images. Here's a tutorial on how to draw a text string using OpenCV:
Ensure you have OpenCV installed:
pip install opencv-python
Import necessary libraries:
import cv2 import numpy as np
You can either create a blank image or load an existing one:
# Create a blank image (white background) image = np.ones((500, 500, 3), np.uint8) * 255 # Or, load an existing image: # image = cv2.imread('path_to_image.jpg')
Use the cv2.putText()
function:
cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])
Where:
img
: The image on which you want to draw the text.text
: The text string to be drawn.org
: Bottom-left corner of the text string in the image (origin of the coordinates).fontFace
: Font type (e.g., cv2.FONT_HERSHEY_SIMPLEX
).fontScale
: Font scale factor.color
: Text color in (B, G, R) format.thickness
(optional): Thickness of the lines used to draw text.lineType
(optional): Line type (default is cv2.LINE_AA
for anti-aliased).bottomLeftOrigin
(optional): If true, the image data origin is at the bottom-left corner. Otherwise, it's at the top-left corner.Example:
text = "Hello, OpenCV!" org = (50, 250) # x, y coordinates of the bottom-left corner of the text fontFace = cv2.FONT_HERSHEY_SIMPLEX fontScale = 1 color = (0, 0, 255) # Red color in BGR format thickness = 2 cv2.putText(image, text, org, fontFace, fontScale, color, thickness, cv2.LINE_AA)
cv2.imshow('Text', image) cv2.waitKey(0) cv2.destroyAllWindows()
Here's a combined script for the entire process:
import cv2 import numpy as np # Create a blank image image = np.ones((500, 500, 3), np.uint8) * 255 # Add text to the image text = "Hello, OpenCV!" org = (50, 250) fontFace = cv2.FONT_HERSHEY_SIMPLEX fontScale = 1 color = (0, 0, 255) # Red in BGR thickness = 2 cv2.putText(image, text, org, fontFace, fontScale, color, thickness, cv2.LINE_AA) # Display the image cv2.imshow('Text', image) cv2.waitKey(0) cv2.destroyAllWindows()
This will display a white image with the text "Hello, OpenCV!" written in red. Adjust the text, coordinates, font, scale, color, and thickness as needed.
Python code for drawing text in OpenCV:
import cv2 import numpy as np # Create a black image img = np.zeros((300, 400, 3), dtype=np.uint8) # Draw text on the image (position, text, font, size, color, thickness) cv2.putText(img, 'Hello OpenCV!', (50, 150), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) # Display the image with the drawn text cv2.imshow('Text Drawing', img) cv2.waitKey(0) cv2.destroyAllWindows()
Text drawing functions and parameters in OpenCV:
# OpenCV putText function signature cv2.putText(img, text, org, fontFace, fontScale, color, thickness, lineType)
text
: Text string to be drawn.org
: Bottom-left corner of the text string.fontFace
: Font type (e.g., cv2.FONT_HERSHEY_SIMPLEX
).fontScale
: Font scale factor.color
: Text color.thickness
: Thickness of the text.lineType
: Type of line for text (e.g., cv2.LINE_AA
for antialiased).Drawing text with specified font, size, and color in OpenCV:
import cv2 import numpy as np # Create a black image img = np.zeros((300, 400, 3), dtype=np.uint8) # Draw text with specified font, size, and color cv2.putText(img, 'OpenCV Text', (50, 150), cv2.FONT_HERSHEY_COMPLEX, 1.5, (255, 0, 0), 2) # Display the image with the drawn text cv2.imshow('Styled Text Drawing', img) cv2.waitKey(0) cv2.destroyAllWindows()
Real-time text drawing in OpenCV:
import cv2 import numpy as np # Create a black image img = np.zeros((300, 400, 3), dtype=np.uint8) # Callback function for keyboard events def draw_text(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: text = 'Coordinates: ({}, {})'.format(x, y) cv2.putText(img, text, (50, 150), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) cv2.imshow('Real-time Text Drawing', img) # Create a named window cv2.namedWindow('Real-time Text Drawing') # Set the callback function for mouse events cv2.setMouseCallback('Real-time Text Drawing', draw_text) while True: # Break the loop on 'Esc' key press if cv2.waitKey(1) & 0xFF == 27: break cv2.destroyAllWindows()
Drawing text on images and videos in OpenCV:
import cv2 # Load an image img = cv2.imread('image.jpg') # Draw text on the image cv2.putText(img, 'Hello OpenCV!', (50, 150), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) # Display the image with the drawn text cv2.imshow('Text on Image', img) cv2.waitKey(0) cv2.destroyAllWindows()
For videos, use the same cv2.putText()
function within a video processing loop.
Combining text drawing with other OpenCV functions:
import cv2 import numpy as np # Load an image img = cv2.imread('image.jpg') # Draw text and a rectangle on the image cv2.putText(img, 'OpenCV Text', (50, 150), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) cv2.rectangle(img, (100, 100), (300, 200), (0, 0, 255), 2) # Display the image with the drawn text and rectangle cv2.imshow('Text and Rectangle Drawing', img) cv2.waitKey(0) cv2.destroyAllWindows()
Text annotations for image labeling in OpenCV:
import cv2 import numpy as np # Load an image img = cv2.imread('object_detection_image.jpg') # Annotate object with text cv2.putText(img, 'Object Detected', (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) # Display the image with the text annotation cv2.imshow('Object Labeling with Text', img) cv2.waitKey(0) cv2.destroyAllWindows()
Drawing multiline text and text with line breaks in OpenCV:
import cv2 import numpy as np # Create a black image img = np.zeros((300, 400, 3), dtype=np.uint8) # Draw multiline text with line breaks text = 'Multiline Text\nLine 2\nLine 3' cv2.putText(img, text, (50, 150), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) # Display the image with the drawn text cv2.imshow('Multiline Text Drawing', img) cv2.waitKey(0) cv2.destroyAllWindows()
Rotating and aligning text in OpenCV:
import cv2 import numpy as np # Create a black image img = np.zeros((300, 400, 3), dtype=np.uint8) # Draw rotated text text = 'Rotated Text' center = (200, 150) angle = 30 scale = 1 cv2.putText(img, text, center, cv2.FONT_HERSHEY_SIMPLEX, scale, (0, 255, 0), 2, cv2.LINE_AA, False) rotated_img = cv2.warpAffine(img, cv2.getRotationMatrix2D(center, angle, scale), (img.shape[1], img.shape[0])) # Display the image with the drawn rotated text cv2.imshow('Rotated Text Drawing', rotated_img) cv2.waitKey(0) cv2.destroyAllWindows()
Adding transparency to drawn text in OpenCV:
import cv2 import numpy as np # Create a transparent image img = np.zeros((300, 400, 4), dtype=np.uint8) img[:, :, 3] = 255 # Set alpha channel to 255 (fully opaque) # Draw transparent text cv2.putText(img, 'Transparent Text', (50, 150), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0, 128), 2) # Display the image with the drawn transparent text cv2.imshow('Transparent Text Drawing', img) cv2.waitKey(0) cv2.destroyAllWindows()
Code examples for drawing text with different styles in OpenCV:
import cv2 import numpy as np # Create a black image img = np.zeros((300, 400, 3), dtype=np.uint8) # Draw regular, italic, and bold text cv2.putText(img, 'Regular', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) cv2.putText(img, 'Italic', (50, 150), cv2.FONT_HERSHEY_SIMPLEX | cv2.FONT_ITALIC, 1, (0, 255, 0), 2) cv2.putText(img, 'Bold', (50, 250), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA, False) # Display the image with the drawn text cv2.imshow('Text with Different Styles', img) cv2.waitKey(0) cv2.destroyAllWindows()