Python OpenCV | cv2.rectangle() method (original) (raw)

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.

Syntax

cv2.rectangle(image, start_point, end_point, color, thickness)

**Parameters:

**Return Value: returns the image with rectangle drawn.

Key Points to Remember

Examples

For the examples, we are using below image:

Example 1: Drawing a Blue Rectangle Border

This 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:

Example 2: Drawing a Filled Black Rectangle on a Grayscale Image

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

ex2_output

**Explanaton:

Common Use Cases

Let’s see some common situations where **cv2.rectangle() is helpful: