How to crop, collage, and create own image by using Code
In this article, I am going to explain each bit about the photo, and how to do operations on top of it by using code.
Before going ahead I want to tell you something here, in this example, I am using Python as a programming language and one of the python library “opencv-python”.
Let’s See Summary -:)
→ How to Load image in python?
→How we can see an image by using python?
→ What is Image in reality?
→ How to use the “cv2” module?
→ How to crop an image and what would be the approach to manually crop?
→ How to collage an image?
→ How to swap the image?
→ How to create an image by code without using Apps from scratch?
Now first set up the environment -:
You should download python to your system if not first install
Now install a library called “opencv-python” as
Now your lab is ready let’s start.
How to Load image in python -:)
Before doing any operation we have to import the module called “cv2”
To load in the ram we have a function
How we can see an image by using python -:
So let’s talk each bit about each function.
cv2.imshow() -: It is a function of cv2 which is used to show the image, but when you this function then your program will crash like
To avoid this we have to use one more function
cv2.waitKey() -: This function is used to wait for the image, the interesting thing here you can set time in milli-second also.
cv2.destroyAllWindows() -: This function is used to after your time complete or you see the image, it is but obvious that you’ll want to quite the image so for this we have to use this function.
Therefore, When we want to see the image we have to use each function at the same time otherwise your program will crash.
What is Image in reality -:)
Image is just a multi-dimensional array, I can prove it but before going ahead I want to show you the programming perspective.
You can see the type of image and shape of image_var.
Now I want to show some very interesting thing let’s see
So first I am opening a photo
See this after this I am going to zoom it then see
See this is only an array.
okay now I want to zoom more and see what is this
See this is a complete array, and each pixel they store RGB color code to show the photo in color.
How to use the “cv2” module -:)
cv2 is a module which is come from the library “opencv-python”
To use we have to first import and according to the use-cases we have to use its function.
How to crop an image -:)
So as you know the photo is just a multi-dimensional array, so if we remove some part of an array then it is but obvious the element of the array will be less,
Before going ahead first see the photo and then I am going to crop it.
Now I want to crop the entire body only I want its face
Let’s crop one more photo
I want to crop its face only
I want to talk about how we can do operation on the array to crop the image so let’s understand it,
See this is a photo, but we know this in a multi-dimensional array so we can do an operation like crop
step 1: I did operation only column, 300 px from left side
step2: I did the operation on the column, 130 px from the right side
step3: Now i did operation on row, 10 px from top
step4: I did the operation on row, 250 px from bottom
This is all about how we can crop on the top of the photo.
How to collage an image -:)
To collage two images we need some extra function, why?
because we know the photo is just an array, so here but obviously we have to add two arrays, we can do with the programming also but in python, we have a function.
In python a function called “numpy” we have a function known as “.hstack” it is used to add two arrays,
So let’s try to collage to image
To image, I read here
See this error
This error tells that
The shape of two individual images is different so when you try to add it but obviously gives an error, so how to overcome this error
Before adding two arrays we have to make the size of two images equal, so the point created here how we can make equal
Here we have to think about what would be a common shape for both
See here I make (600*960) image shape, now i am again going to add
Let’s open the collage_image
See this is a two independent photo I collage each other.
How to swap the image -:)
Note -: Before swap check the image shape if not then make it equal as I discussed above.
To swap what we can do, we can split a photo in two-part and add half part with another half part photo like
Let’s print the photo,
Let’s again swap with another part,
This is all about the swapping.
How to create an image by code without using Apps -:)
We know that image is just like array and behind the scen it store the color code so thats why we see photo in color,
So what should be approach when we try to create image by code.
We can creat a multi-dimensonal array and all value we can start with 0 like
cv2 -: we used to do because we want to show image.
numpy -: we used to create a blank photo
numpy have a function “zeros()” which is used to creat a blank array, here i have created a 3D array so that why i wriiten 3 and also to put the color.
Let’s try to see blank image
Now this time to do some manupulation to change the color
Here we have added a color on some part of this image,
Note -:) In cv2 they support color in “BGR” format not “RGB”
Again i did operation let’s see how it looks like,
Like this you can do lots more operations or manupulation on the array, i coverd each about the image and i gives you core concept about the image.
Keep Sharing Keep Learning
MD MOQADDAS