Creating Image by using cv2 python && Cropping the images && Combining two images
Task1 : Creating an image using cv2 python code.
Please find the code as below:
import numpy as np
img2 = np.zeros((880,320,3),np.uint8)
img2[0:440, :] = (255,0,255)
img2[441:880, :] = (255,255,0)
buffer2[:, :, :] = img2[:, :, :]
Here in the code you can see two modules cv2 and numpy are imported.
Initially in the new image every pixel should be set to zero. After that uint8 will make each pixel to get ready in the form of R,G,B format.
We have created an empty image of 880 rows and 320 columns.
In the top half of image I filled it with pink color and the bottom half is filled with green color.
I am also taking a copy of this image img2 as buffer2.
Now, to view the image we are using imshow function and then to keep on hold until we close it , we are using waitkey and destroyallwindows functions.
Task2: Cropping the images
Lets create an another image or read an existing image fron my local folder as below:
pic5 = cv2.imread(“PROFILE_PIC3.jpg”) // This statement helps in reading an another image from my local laptop.
buffer1 = cv2.imread(“PROFILE_PIC3.jpg”) // Just creating a buffer copy of read image
a5= pic5[144:584,663:983,:] //The face part of image coordinates are found by haarcascade face detection filter. Then store those crucial array information into some variable.
b5= img2[0:440,0:320,:] // The image which we created above , I am taking some part of information
buffer2[0:440, 0:320, :] = a5 //Now store that a5 into buffer2 copy of image1
buffer1[144:584,663:983,:] = b5 // Now store the b5 copy into buffer1 image.
Now try to open both images. You can see they are cropped.
Now you can see that both those image are cropped.
Task3: Combining both the images
Code is as follows:
import numpy as np
img3 = np.zeros((440,700,3),np.uint8)
img3[0:220, :] = (255,0,255)
img3[221:440, :] = (255,255,0) // The above commands will help us to create an image which is 440 x 700 sized array and fill above half with pink and below half with blue color.
pic6 = cv2.imread(“National-Flag-of-India-ili-59-img-2.jpg”) // Read a national flag of size 440 x 700
combine = np.hstack( (img3,pic6)) // As both the images are of same size, then try to perform a horizontal stack and combine both the aarays.
Here the output is as follows: