Create image , Swap parts of two images , Make a Collage with Python CV2
What is an Image ?
Image is nothing but a 3 dimensional array with depth as 3, where each element of array is a combination of RGB (Red Blue Green). Each array element is represented as 1 pixel.
Let’s get to work.
Creating our own image in Python :
First, install the library “opencv-python” with the command :
pip install opencv-python #For windows
pip3 install opencv-python #For linux
Go to the Jypyter Notebook or any other IDE and import the modules :
import cv2
import numpy
Initialize the image with the color Black. This can be done with numpy.zeros() function as follows :
arr = numpy.zeros((400,600,3))
This creates a black color image of resolution : 600 x 400.
The following snippet is used for displaying any array as an image in python :
cv2.imshow('hi',arr)
cv2.waitKey()
cv2.destroyAllWindows()
The waitKey() function close the image when we press any key or cancel button in GUI.
The destroyAllWindows() function closes the window afterwards.
When above snippet is executed, a black image will be displayed.
Now, we draw some shapes over the black image.
Add the following code and run it. This code will draw a vertical rectangle at the position specified as shown below :
arr[130:270 , 170:200] = [255,255,255]
Simlarly, add the following code :
arr[130:270 , 170:200] = [255,255,255]
arr[190:210 , 200:250] = [255,255,255]
arr[130:270 , 250:280] = [255,255,255]
arr[130:270 , 310:340] = [255,255,255]
arr[130:140 , 300:350] = [255,255,255]
arr[260:270 , 300:350] = [255,255,255]
arr[130:230 , 400:420] = [0,255,255]
arr[250:270 , 400:420] = [255,255,0]
Display the array using the snippet :
cv2.imshow('hi',arr)
cv2.waitKey()
cv2.destroyAllWindows()
You can view the final image which displays the Letters : HI !
You have created your own image !
You can save your image with :
cv2.imwrite('hi.jpg',arr)
Crop some part of 2 images and swap them :
I’m taking 2 images for this purpose :
Import the cv2 module and import the above 2 images :
import cv2
photo1 = cv2.imread('photo1.png')
photo2 = cv2.imread('photo2.png')
You can view the dimensions of photos with the “shape” :
photo1.shape
photo2.shape
For the images, I took the dimension is : (512, 512, 3).
Now, get a part of photo2 and store it in a variable :
(I took the vertical second half of the photo2. 256 is half of 512 which is the actual size of the photo2)
photo2_crop = photo2[:, 256: ]
Now, replace the vertical second half of photo1 with the value of the variable : photo2_crop :
photo1[:, 256: ] = photo2_crop
Now, display the photo1 with the snippet :
cv2.imshow('photo1',photo1)
cv2.waitKey()
cv2.destroyAllWindows()
You can view the magic :
Similarly do it for the second image :
photo1 = cv2.imread('photo1.png')
photo1_crop = photo1[:, 256: ]
photo2[:, 256: ] = photo1_cropcv2.imshow('photo2',photo2)
cv2.waitKey()
cv2.destroyAllWindows()
You can view the image :
Now, you successfully swapped the cropped parts of 2 images.
Making a Collage from two images :
I’m using the following 2 images for this task :
Import the modules :
import cv2
import numpy
Read the images :
photo1 = cv2.imread('1.png')
photo2 = cv2.imread('2.png')
Know the shapes of the images :
photo1.shape
photo2.shape
For the images I took, the shape is: (214, 300, 3)
If you understand, photo1 is one array and photo2 is another array. In python we can combine two arrays horizontally with the help of “hstack” function. We are doing the same thing now :
Collage = numpy.hstack((photo1 , photo2))
Now, Collage is an array which contains the arrays : photo1 and photo2 attached horizontally. View the image with the code :
cv2.imshow('Collage', Collage)
cv2.waitKey()
cv2.destroyAllWindows()
You will view the following collage :
You have successfully made a collage !!
The entire code can be found at GitHub :