A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-opencv-cv2-rectangle-method/ below:

Python OpenCV | cv2.rectangle() method

Python OpenCV | cv2.rectangle() method

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

Explanaton:

Common Use Cases

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