Crop and Swap 2 images using Python

Abhay desai
3 min readJun 5, 2021

Step 1: Install cv2 Module.

command : pip install opencv-python.

|| OpenCV (Open Source Computer Vision Library) is a library of programming functions mainly aimed at real-time computer vision. ||

Step 2: Import cv2 and Numpy

Importing cv2 module and Importing numpy as np

Step 2 : Reading the First and Second photo

synatx : cv2.imread(‘filename’)

Step 3: Lets check Shape of the photo

synatx : variablename.shape

Shape of the 1 image
Shape of the second image

Step 4 : Lets Join both the image horizontally:

As we have imported numpy as np so we have join horizontally by using

np.hstack(img1 , img2)

Step 5 : Lets check the Image

Syntax: cv2.imshow(window_name, image) #display image
cv2.waitkey(time in ms) # wait for a specific time in milliseconds
cv2.destroyAllWindows() # destroy the window after displaying the image

Output :

Step 6 : Crop the Second Photo

crop the Second Photo.

Swap the Photo.

and saved as the final_photo1.

Step 7 : Crop the First Photo

Crop the First Photo

Swap the part with the second photo.

and Saved as the Final_photo2

Step 8 : Again we will join the Photo Horizontally

As we have got the output

we will merge the image horizontally and save the image in variable name photo

Step 9 : Output:

Output:

Hope you Find it Intresting ..!!

Thank you ..!!

--

--