Playing with images using opencv

Rudraksh Dash
4 min readJun 12, 2021

Hi readers!! welcome to my blog.

đź‘€Human eyes can percieve what it sees. We have so much developed minds that we try to form patterns as soon as we start seeing things infront of us.But computer lacks this ability to see.For that OpenCV library helps the computer to see things through various functions provided by this library.For images we use cv2.imread() command to read those images from the disk and cv2 in this process reads the image as a combination of numbers ie array.It comes with numpy array preinstalled with but works for cv2 commands.Any other operations on numbers we have to import numpy explicitly.

What we see in smartphones like an option of CROP in photos/gallery app is basically a piece of code running behind. In this blog we are gonna achieve the following tasks :-
1) To create our own image in python.
2) To CROP a photo and swap to any other image.
3) To combine 2 images to form a collage.

TASK — 1

STEP1) TO CHECK IF OpenCV-python library is present in our system. In my case it is there.
IF LIBRARY NOT PRESENT this command will download and install it.

STEP2) We import cv2 library and numpy for making an image. We create a 3d list to store values for our colourful image and fill it using a simple loop function. Now these list values are not in array format so we convert it as seen in line 5 and we further change type it into uint8.Now our array is ready tobe used as image and we call cv2.imshow() to show us the image.

DONE

TASK — 2

S1) lets download 2 images on our system first.

S2) Now we read the files using imread()

S3) Now we first identify our verices for cropping.

similary we do for the second image.

S4) Now we crop those points using slicing operator and attach the cropped part to Elons image and use imshow set of commands to show us the “cropped image”

AND HERE IT IS!!!!

TASK — 3
steps — Here we need to be cautious about one thing that is the working of numpy append which we will use to combine two of our images.For that we first check our photo sizes

Now according to appending rules the dimension along the concatenation axis needs to be the same for all the input arrays. Here the dim of photo2 is 261,450,3 but dim of photo1 is (524,700,3) therefore we need to crop our image 1 accordingly.

AND WE R DONE!!!

THANKS FOR A READ!!

SEE YOU ALL SOON!

--

--

Rudraksh Dash
0 Followers

Trainee at LinuxWorld Informatics Pvt Ltd. Having interest in devops and ML