The OpenCV can also allow us to edit the images by drawing on them or writing texts on them.
Create Blank Image:
We can draw and write on an existing image file or create a blank image and edit it.
import cv2 as cv
import numpy as np
# Create a blank image
blank = np.zeros((500,500,3), dtype='uint8')
cv.imshow('Blank', blank)
cv.waitKey(0)
To create a blank image, we need to use the “np.zeros((500,500,3), dtype=’uint8′)”
np.zeros((500,500,3), dtype='uint8')
: This part is a call to the zeros
function from the NumPy library, which generates an array filled with zeros.
(500,500,3)
: This is a tuple specifying the shape of the array.500, 500
: These are the dimensions of the image, meaning it will be 500 pixels wide and 500 pixels tall.3
: This indicates the number of channels in the image. For a typical RGB image, there are 3 color channels: Red, Green, and Blue.
dtype='uint8'
: This sets the data type of the array elements touint8
, which stands for “unsigned 8-bit integer”. This is a common data type for image arrays, as it allows pixel values to range from 0 to 255. It is basically a datatype for an image.

Paint image
# 1. paint the whole image a certain color:
blank[:] = 0,255,0
cv.imshow('Green', blank)
cv.waitKey(0)
The blank[:] select all the pixels in the image and the color 0,255,0 which is green is assigned to it.

# 2. paint part of an image a certain color:
blank[200:300, 200:300] = 0,0,255
cv.imshow('Red Square', blank)
cv.waitKey(0)
The blank[200:300, 200:300] assigns the selected range of pixels to change color.
Draw Shapes
# 3. draw a rectangle:
cv.rectangle(img=blank, pt1=(0,0), pt2=(250,500), color=(0,255,0), thickness=cv.FILLED) # this fills the shape with the given color
cv.rectangle(img=blank, pt1=(0,0), pt2=(250,500), color=(0,255,0), thickness=2)
cv.imshow('Rectangle', blank)
cv.waitKey(0)
The cv.rectangle method can help me draw a rectangle, I just need to tell the computer the image I want to draw on, the upper left and lower right point of the shape, the color of the shape, and the thickness of the pen.

# 4. Draw a Circle:
cv.circle(img=blank, center=(blank.shape[1]//2 , blank.shape[0]//2), radius=100, color=(0,255,0), thickness=2)
cv.circle(img=blank, center=(250,250), radius=100, color=(0,255,0), thickness=cv.FILLED)
cv.imshow('Circle', blank)
cv.waitKey(0)
The cv.circle method help me to draw a circle, similar to the rectangle method but I need to provide the center and radius this time.

# 5. Draw a Line:
cv.line(img=blank, pt1=(0,0), pt2=(250,250), color=(255,255,255), thickness=10)
cv.imshow('Line', blank)
cv.waitKey(0)
The cv.line method can help me to draw a line on the image by telling the computer where the line start and end.

Write Texts
# 6. Write text on an image:
cv.putText(img=blank, text='Hello World', org=(225,225), fontFace=cv.FONT_HERSHEY_TRIPLEX, fontScale=1.0, color=(255,255,255), thickness=2)
cv.imshow('Text', blank)
cv.waitKey(0)
To write texts on images, I need to use the cv.putText method, I need to tell the computer the text I want to put, the origin of where the text should start writing, and the font.
