Draw geometric shapes on images using Python OpenCv module

OpenCV provides powerful functions to draw geometric shapes on images. This capability is essential for image analysis, annotation, and visualization tasks where you need to highlight specific regions or add visual markers.

Creating a Blank Canvas

First, let's create a blank image using NumPy to serve as our canvas ?

import numpy as np
import cv2

# Create a blank black image (350x350 pixels, 3 channels for BGR)
my_img = np.zeros((350, 350, 3), dtype="uint8")
cv2.imshow('Window', my_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Drawing Lines

The cv2.line() function draws straight lines between two points. It accepts these parameters ?

  • Image object − The canvas to draw on
  • Start point − Coordinates (x, y) of line start
  • End point − Coordinates (x, y) of line end
  • Color − BGR color tuple (not RGB)
  • Thickness − Line width in pixels
import numpy as np
import cv2

my_img = np.zeros((350, 350, 3), dtype="uint8")
# Draw a line from (202,220) to (100,160) in red color
cv2.line(my_img, (202, 220), (100, 160), (0, 20, 200), 10)
cv2.imshow('Window', my_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Drawing Rectangles

The cv2.rectangle() function creates rectangular shapes using opposite corner coordinates ?

  • Image object − The canvas to draw on
  • Top-left corner − Coordinates (x, y)
  • Bottom-right corner − Coordinates (x, y)
  • Color − BGR color tuple
  • Thickness − Border width (-1 fills the shape)
import numpy as np
import cv2

my_img = np.zeros((400, 400, 3), dtype="uint8")
# Draw a rectangle from (30,30) to (300,200)
cv2.rectangle(my_img, (30, 30), (300, 200), (0, 20, 200), 10)
cv2.imshow('Window', my_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Drawing Circles

The cv2.circle() function creates perfect circles from a center point and radius ?

import numpy as np
import cv2

my_img = np.zeros((400, 400, 3), dtype="uint8")
# Draw a circle at center (200,200) with radius 80
cv2.circle(my_img, (200, 200), 80, (0, 20, 200), 10)
cv2.imshow('Window', my_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Drawing Ellipses

The cv2.ellipse() function creates elliptical shapes with customizable axes and rotation ?

import numpy as np
import cv2

my_img = np.zeros((400, 400, 3), dtype="uint8")
# Draw ellipse: center(256,256), axes(100,50), angle=0, start=0°, end=180°
cv2.ellipse(my_img, (256, 256), (100, 50), 0, 0, 180, (255, 255, 255), -1)
cv2.imshow('Window', my_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Drawing Polygons

The cv2.polylines() function connects multiple points to create polygon shapes ?

import numpy as np
import cv2

my_img = np.zeros((400, 400, 3), dtype="uint8")
# Define polygon points
pts = np.array([[10, 5], [20, 30], [70, 20], [50, 10]], np.int32)
pts = pts.reshape((-1, 1, 2))
cv2.polylines(my_img, [pts], True, (0, 255, 255), 3)
cv2.imshow('Window', my_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Adding Text

The cv2.putText() function renders text on images with customizable fonts and styling ?

import numpy as np
import cv2

my_img = np.zeros((400, 400, 3), dtype="uint8")
# Add text with specified font and properties
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(my_img, 'OpenCV Shapes', (50, 200), font, 1.2, (255, 255, 255), 2, cv2.LINE_AA)
cv2.imshow('Window', my_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Key Points

  • OpenCV uses BGR color format instead of RGB
  • Negative thickness (-1) fills shapes completely
  • cv2.LINE_AA provides anti-aliased smooth lines
  • All coordinates use (x, y) format with origin at top-left

Conclusion

OpenCV's drawing functions provide comprehensive tools for adding geometric shapes and text to images. These functions are essential for image annotation, computer vision applications, and creating visual overlays on processed images.

Updated on: 2026-03-25T05:02:09+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements