Image Creation, Swapping, and Combining using Python
Hey Folks! Let’s do something creative. Today we are going to create an image, swap two images and combine two images, but not through photoshop or any other tool, but through Python code. So let the Fun Begin!
https://drive.google.com/drive/folders/1XCe0U6TcSAlvAKhT1yj16Sri1c3AyFm5?usp=sharing
The above drive link contains all the images being used in the entire task. Kindly refer the link to access the images and run the code in your notebook, Have FUN!!
Steps:import cv2
Here we import the openCV library. As computers don’t understand what images are and they only work with numbers, we create an image using numpy arrays for which we here import the library and perform necessary functions.
import numpy as npphoto_a = np.random.uniform(size=(256,256,3))*255photo_a = np.round(photo_a)photo_a[10:20,50:-50]= [0,0,0]photo_a[20:200,120:-120]= [0,0,0]photo_a[100:110,50:-50]= [0,0,0]photo_a[190:200,50:-50]= [0,0,0]photo_a[10:200,50:60]= [0,0,0]photo_a[10:200,-60:-50]= [0,0,0]photo_a[20:100,60:120] = [0,0,256]photo_a[20:100,135:-60] = [0,256,256]photo_a[20:100,135:-60] = [0,256,256]photo_a[110:190,60:120] = [0,256,0]photo_a[110:190,135:-60] = [256,0,0]#This is our image in form of array.photo_a
The below pic shows the image in array form
Now,we save the image over here in our folder
cv2.imwrite(“new.jpg”, photo_a)
Using necessary commands we here now display the image.
photo_a = cv2.imread(“new.jpg”)cv2.imshow(“new pic”, photo_a)cv2.waitKey()cv2.destroyAllWindows()
AND
We are now going to swap images, where we will add another image in an image
Step 1:Getting the images.Trust me, it’s funny
photo1 = cv2.imread(“mona-lisa-copy_custom-cf935c261c640b9ff7e214059a0328c880c22f50-s800-c85.jpg”)photo2 = cv2.imread(“1mj0gg.jpg”)photo1.shape
Now,cropping the images to make sure they fit in each other, we now add the image.
photo1 = photo1[0:512,0:686]photo2 = photo2[0:600,0:1920]cv2.imshow(“Pic1”, photo1)cv2.imshow(“Pic2”, photo2)cv2.waitKey()cv2.destroyAllWindows()
Lets swap the image
photo2[0:512,700:700+686] = photo1
Found it funny? lol
cv2.imshow(“Damn”, photo2)cv2.waitKey()cv2.destroyAllWindows()
NOW
We will now make a collage. As earlier said, images are only bunch of numbers to computer, so as learned in numpy, to stack images we will basically stack arrays
photo3 = cv2.imread(“modi-looking-at-shah.jpg”)photo4 = cv2.imread(“18syng.jpg”)photo4.shape
See images before stacking
photo3 = photo3[0:400,0:700]cv2.imshow(“pic1”, photo3)cv2.imshow(“pic2”, photo4)cv2.waitKey()cv2.destroyAllWindows()
See images after stacking
photo5 = np.hstack((photo3,photo4))cv2.imshow(“Damn”, photo5)cv2.waitKey()cv2.destroyAllWindows()
ANDDD…The task is completed!
Thank You..!! Will be back with more fun.