While playing with images in OpenCV we can do various fun stuff using its inbuilt functions. One of them is **drawing basic shapes like circles, rectangles, ellipses, and lines on images**.

In today’s very short blog we will see that how we can draw these basic shapes on a black image. This is going to be a very interesting blog, so without any further due, Let’s do it…

## Step 1 – Import the required packages.

import cv2 import numpy as np

## Step 2 – Create a Black image.

img = np.zeros((512,512,3), np.uint8)

- Yes, you read that right. We can create an image also in OpenCV on which we will draw our shapes.
- What this line will do is, it will create a 3-D array of all zeros with height and width both set to 512 pixels. The three dimensions here depict RGB channels.
- When all these are 0 it will act as a black image because we know that (0,0,0) is black and (255,255,255) is white. Every pixel of this image will be (0,0,0).

## Step 3 – Visualize our black image.

cv2.imshow('Black Image', img) cv2.waitKey(0)

## Let’s draw our shapes…

### Line

**Syntax** – cv2.line(image, start_point, end_point, color, thickness)

**Parameters:**

image | It is the image on which line is to be drawn. |

start_point | It is the starting coordinates of the line. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value). |

end_point | It is the ending coordinates of line. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value). |

color | It is the color of the line to be drawn. For BGR, we pass a tuple. eg: (255, 0, 0) for blue color. |

thickness | : It is the thickness of the line in px. |

cv2.line(img, (0, 0), (512, 512), (0, 225, 0))

### Rectangle

**Syntax:** cv2.rectangle(image, start_point, end_point, color, thickness)

**Parameters:**

image | It is the image on which a rectangle is to be drawn. |

start_point | It is the starting coordinates of the rectangle. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value). |

end_point | It is the ending coordinates of the rectangle. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value). |

color | It is the color of the borderline of the rectangle to be drawn. For BGR, we pass a tuple. eg: (255, 0, 0) for blue color. |

thickness | It is the thickness of the rectangle borderline in px. The thickness of -1 px will fill the rectangle shape by the specified color. |

cv2.rectangle(img, (20, 40), (160, 140), (225, 225, 225), -1)

### Circle

**Syntax:** cv2.circle(image, center_coordinates, radius, color, thickness)

**Parameters:**

image | It is the image on which a circle is to be drawn. |

center_coordinates | It is the center coordinates of circle. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value). |

radius | It is the radius of circle. |

color | It is the color of border line of circle to be drawn. For BGR, we pass a tuple. eg: (255, 0, 0) for blue color. |

thickness | It is the thickness of the circle border line in px. Thickness of -1 px will fill the circle shape by the specified color. |

cv2.circle(img, (206, 206), 50, (225, 225, 225), -1)

### Ellipse

**Syntax:** cv2.ellipse(image, center coordinates, axesLength, angle, startAngle, endAngle, color [, thickness[, lineType[, shift]]])

**Parameters:**

image | It is the image on which ellipse is to be drawn. |

center_coordinates | It is the center coordinates of ellipse. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value). |

axesLength | It contains tuple of two variables containing major and minor axis of ellipse (major axis length, minor axis length). |

angle | Ellipse rotation angle in degrees. |

startAngle | Starting angle of the elliptic arc in degrees. |

endAngle | Ending angle of the elliptic arc in degrees. |

color | It is the color of border line of shape to be drawn. For BGR, we pass a tuple. eg: (255, 0, 0) for blue color. |

thickness | It is the thickness of the shape border line in px. Thickness of -1 px will fill the shape by the specified color. |

lineType | This is an optional parameter.It gives the type of the ellipse boundary. |

shift | This is an optional parameter. It denotes the number of fractional bits in the coordinates of the center and values of axes. |

cv2.ellipse(img, (380, 380), (50, 20), 0, 0, 360, (225, 225, 225), -1)

** NOTE**: You can read more about drawing shapes here.

Do let me know if there’s any query regarding this topic by contacting me on email or LinkedIn.

*So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…*

**Read my previous post: HOW TO RESIZE AN IMAGE IN PYTHON USING OPENCV**

**Check out my other machine learning projects, deep learning projects, computer vision projects, NLP projects, Flask projects at machinelearningprojects.net**.