Python | Draw geometric shapes on image using OpenCV

In this opencv tutorial I will show you how to draw geometric shapes on image using opencv. For this tutorial I will draw geometric shapes on black image, you can always draw anything on any image by reading this article.

Why you need to draw shapes on image? Let’s say you are doing object detection, there you need to draw bounding box on image to mark that object by any shape.

In my last post I have shown you some basic functions which you need almost every day to learn OpenCV. To draw shapes on image you will also need some functions. We are going to use below functions in this post:

  • line(): opencv draw line python
  • rectangle(): Python draw rectangle on image
  • circle(): To draw circle
  • putText(): Write text on a image opencv

OpenCV draw line python

Let’s first create a black image (256,256), then I will draw line in python on that black image. I will draw all geometric shapes on that black image.

In opencv draw line by below code:



  • img: The image on which you are going to draw line
  • (0,0): Starting point of line
  • (255,255): Ending point of line
  • (255,0,0): Color of line (blue)
  • 2: Thickness

If you are now sure about X and Y axis, I will suggest you to read my previous post, there I have explained how OpenCV see an image.

Python draw rectangle on image

To draw rectangle on image using opencv you can use below code:

cv2.rectangle(img,(starting_x, starting_y),(ending_x, ending_y),(0,0,255),3)


  • img: The image on which you are going to draw line
  • (starting_x, starting_y): Starting point of rectangle
  • (ending_x, ending_y): Ending point of rectangle
  • (0,0,255): Color of rectangle (red)
  • 3: Thickness
Also Read:  Capturing mouse click events with Python and OpenCV

This is the way you can draw bounding box on image.
Now you can also draw filled rectangle by replacing thickness (3) with cv2.FILLED.

cv2.rectangle(img,(starting_x, starting_y),(ending_x, ending_y),(0,0,255),cv2.FILLED)

Draw circle on image OpenCV python

To draw circle in python opencv you just need to mention centre point, radious and thickness.,(centre_x, centre_y),50,(0,255,0),cv2.FILLED)


  • img: The image on which you are going to draw line
  • (centre_x, centre_y): Centre point of the circle
  • (0,255,0): Color of the circle (green)
  • cv2.FILLED: filled circle

Write text on image OpenCV

To write text on an image you can use below code:

cv2.putText(img,"Draw Shapes",(25,50),cv2.FONT_HERSHEY_DUPLEX,1,(30, 255, 255),1)


  • img: The image on which you are going to draw line
  • “Draw Shapes”: Text to display
  • (25,50): Center of the circle
  • cv2.FONT_HERSHEY_DUPLEX: Font of the text
  • 1: Scale of the text
  • (30, 255, 255): Color of the text (yellow color code OpenCV)
  • 1: Thickness

Full code

Now putting all together:

import cv2
import numpy as np

# Creating black image of (256 X 256), 3 is ro BGR
# Creating 8 bit integer value by unit8
img = np.zeros((256,256,3),np.uint8)

# To create a blue image you can use below commented code
# img[:] = 255 ,0,0

# --------------------------------------------
starting_x = 150
starting_y = 75
ending_x = 225
ending_y = 125
cv2.rectangle(img,(starting_x, starting_y),(ending_x, ending_y),(0,0,255),3)
# opencv draw filled rectangle
# cv2.rectangle(img,(starting_x, starting_y),(ending_x, ending_y),(0,0,255),cv2.FILLED)
# --------------------------------------------
centre_x = 75
centre_y = 175,(centre_x, centre_y),50,(0,255,0),cv2.FILLED)
cv2.putText(img,"Draw Shapes",(25,50),cv2.FONT_HERSHEY_DUPLEX,1,(30, 255, 255),1)
# --------------------------------------------
cv2.imshow("Geometric shapes", img)

Draw geometric shapes on image using python OpenCV


In this post you learned:

  • Draw bounding box on image python OpenCV
  • OpenCV draw filled rectangle
  • OpenCV draw line python
  • python draw rectangle on image
  • Draw rectangle on video OpenCV python

If you have any question or suggestion regarding this topic see you in comment section. I will try my best to answer.

185 thoughts on “Python | Draw geometric shapes on image using OpenCV”

Leave a comment