CREATING IMAGE USING PYTHON CODE
3 min readJun 4, 2021
PRE-REQUITIES ::
* Install cv2 modulecommand: pip install OpenCV-python
|| OpenCV is a cross-platform library using which we can develop real-time computer vision applications.
**It mainly focuses on image processing, video capture and analysis including features like face detection and object detection.
|| OpenCV-Python makes use of Numpy , which is a highly optimized library for numerical operations with a MATLAB-style syntax.
1.Import cv2 and numpy.
2. Create an Blank image using numpy.
command : numpy.zeros((height , width , 3))* numpy.zeros : fills with zero which means black color
i.e., (B , G , R) = (0 , 0 , 0)* '3' : BGR color
3. Start to draw the image as per your wish using some functions.
A. To draw any shapes * Use "drawContours()" with boundary pointsSYNTAX: cv2.drawContours(<img>, <contours>, <contourIdx>, <colour>, <thickness>)
Where
- img : Name of the image
- contours : All the input contours.
- contourIdx : Parameter indicating a contour to draw. If it is negative, all the contours are drawn.
- color : Color of the contours (B , G , R)
- thickness is how thick are the lines drawing the contour
* To specify boundary points use "array()"
SYNTAX: numpy.array()
B. To draw circle on image : cv2.circle()SYNTAX: cv2.circle(<img>, (‘center_coordinates’), (‘circle_radius’), (‘color_in_bgr’), ‘stroke_thickness’)
C. To draw rectangle on image : cv2.rectangle()SYNTAX: cv2.rectangle(imageObjectName, (‘top_left_vertex_coordinates’), (‘lower_right_vertex_coordinates’), (‘stroke_color_in_bgr’), ‘stroke_thickness’)
D. To draw line on image : cv2.line()SYNTAX: cv2.line(imageObjectName, (‘start_coordinates’), (‘end_coordinates’), (‘color_in_bgr’), ‘line_thickness’)
4. To write on image
SYNTAX: cv2.putText(imageObjectName, ‘TextContent’, (‘text_starting_point_coordinates’), ‘fontToBeUsed’, ‘font_size’, (‘text_color’, ‘text_thickness’, ‘line_type’)
5. Now see the output.
Syntax: cv2.imshow(window_name, image)
cv2.waitkey(time in ms)
cv2.destroyAllWindows()
FINAL CODE :