Learn how to use Python's OpenCV library (cv2) to easily draw rectangles with custom text overlays on images and videos.
This code demonstrates how to use the OpenCV library in Python to draw a rectangle and add text on an image.
import cv2
# Load the image
image = cv2.imread('image.jpg')
# Define the rectangle coordinates
x1, y1 = 100, 50
x2, y2 = 200, 150
# Draw the rectangle on the image
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Define the text and its position
text = "Hello, OpenCV!"
text_position = (x1, y2 + 30)
# Put the text on the image
cv2.putText(image, text, text_position, cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# Display the image
cv2.imshow('Image with Rectangle and Text', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Explanation:
import cv2
image = cv2.imread('image.jpg')
x1, y1 = 100, 50
and x2, y2 = 200, 150
cv2.rectangle()
:
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
text = "Hello, OpenCV!"
text_position = (x1, y2 + 30)
cv2.putText()
:
cv2.putText(image, text, text_position, cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
cv2.imshow('Image with Rectangle and Text', image)
cv2.waitKey(0)
waits for a key press to close the image window.cv2.destroyAllWindows()
closes all OpenCV windows.The Python code uses the OpenCV library to load an image, draw a green rectangle on it, add the text "Hello, OpenCV!" below the rectangle in red, and then display the modified image in a window.
import cv2
# Load the image
image = cv2.imread('image.jpg')
# Define the rectangle coordinates
x1, y1 = 100, 50
x2, y2 = 200, 150
# Draw the rectangle on the image
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Define the text and its position
text = "Hello, OpenCV!"
text_position = (x1, y2 + 30)
# Put the text on the image
cv2.putText(image, text, text_position, cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# Display the image
cv2.imshow('Image with Rectangle and Text', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Explanation:
Import the OpenCV library: import cv2
Load the image: image = cv2.imread('image.jpg')
imread()
function. Make sure you replace "image.jpg" with the actual filename of your image.Define the rectangle's coordinates:
x1, y1 = 100, 50
: These variables define the x and y coordinates of the top-left corner of the rectangle.x2, y2 = 200, 150
: These variables define the x and y coordinates of the bottom-right corner of the rectangle.Draw the rectangle: cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
rectangle()
function.image
: The image on which to draw the rectangle.(x1, y1)
: Top-left corner coordinates.(x2, y2)
: Bottom-right corner coordinates.(0, 255, 0)
: Color of the rectangle in BGR (Blue, Green, Red) format. Here, it's green.2
: Thickness of the rectangle's border in pixels.Define the text and position:
text = "Hello, OpenCV!"
: The text string to be displayed.text_position = (x1, y2 + 30)
: The position of the text's bottom-left corner. It's placed 30 pixels below the rectangle.Put the text on the image: cv2.putText(image, text, text_position, cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
putText()
function.image
: The image on which to put the text.text
: The text string.text_position
: Position of the text.cv2.FONT_HERSHEY_SIMPLEX
: Font type.1
: Font scale (size).(0, 0, 255)
: Color of the text in BGR (red in this case).2
: Thickness of the text.Display the image:
cv2.imshow('Image with Rectangle and Text', image)
: This line displays the image in a window titled "Image with Rectangle and Text".cv2.waitKey(0)
: This line waits indefinitely for a key press. This allows you to see the image until you press any key.cv2.destroyAllWindows()
: This line closes all OpenCV windows after you press a key.This code demonstrates how to use OpenCV to draw basic shapes like rectangles and add text to images. You can modify the coordinates, colors, text, and other parameters to customize the output.
cv2.rectangle()
will fill the rectangle.cv2.putText()
like cv2.FONT_HERSHEY_PLAIN
, cv2.FONT_HERSHEY_COMPLEX
, etc.if image is None:
print("Error: Could not load image.")
exit()
This Python code uses the OpenCV library to:
The code demonstrates basic image manipulation techniques in OpenCV, including drawing shapes and adding text.
This code provides a foundational understanding of how to use OpenCV for basic image manipulation tasks. By modifying the provided code, you can explore drawing other shapes, adding text in different styles, and experimenting with various image processing techniques offered by the OpenCV library.