Drawing Shapes on Images with OpenCV in Python

This tutorial demonstrates how to use OpenCV in Python to read an image and draw basic shapes such as rectangles and circles by specifying coordinates, dimensions, colors, and line thickness, then display the edited image in a window.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Drawing Shapes on Images with OpenCV in Python

OpenCV is a powerful library that enables arbitrary image editing and processing, making it suitable for computer‑vision tasks.

The following example shows how to specify the stroke size and draw basic geometric shapes on an image using Python.

Example: Drawing Shapes

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time    : 2021/7/17 下午9:53
# @Author  : huaan

import cv2 as cv

img = cv.imread("one.jpg")
# Draw rectangle
# Top‑left corner (x, y), width w and height h
x, y, w, h = 100, 100, 100, 100
cv.rectangle(img, (x, y, x + w, y + h), color=(0, 255, 0), thickness=2)  # BGR

# Draw circle
x, y, r = 200, 200, 100
cv.circle(img, center=(x, y), radius=r, color=(0, 255, 255), thickness=2)

# Display image
cv.imshow("rectangle_img", img)
cv.waitKey(0)
cv.destroyAllWindows()
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Computer VisionPythonImage ProcessingOpenCVDrawing Shapes
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.