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.
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()Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
