Last Updated : 14 Aug, 2025
cv2.rectangle() function in OpenCV is used to draw a rectangle shape on an image. By specifying starting and ending coordinates, color and thickness, you can highlight or mark specific areas in an image, which is helpful for tasks like annotation, object detection and image processing visualization.
Syntaxcv2.rectangle(image, start_point, end_point, color, thickness)
Parameters:
Return Value: returns the image with rectangle drawn.
Key Points to RememberFor the examples, we are using below image:
Example 1: Drawing a Blue Rectangle BorderThis code reads an image, draws a blue rectangle on it and displays the modified image in a window.
Python
import cv2
image = cv2.imread('logo.png')
cv2.rectangle(image, (5, 5), (220, 220), (255, 0, 0), 2)
cv2.imshow('Image', image)
cv2.waitKey(0) # Waits indefinitely until a key is pressed.
cv2.destroyAllWindows() # Closes all windows opened by program.
Output
Explanation:
This example reads an image in grayscale and draws a filled black rectangle on it, then displays result in a window.
Python
import cv2
image = cv2.imread(r'logo.png', 0)
cv2.rectangle(image, (100, 50), (125, 80), 0, -1)
cv2.imshow('Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output
Explanaton:
Let’s see some common situations where cv2.rectangle() is helpful:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4