In this article, We are going to see how to create a coloured image using Python. Not only that, we are also going to see how to make changes in that image which includes Crop, Swap and collate the images.
In Simple words, an Image is just a 2D/3D array. If it is grey image, then it is a 2D array and if it is a color image, then it is a 3D array.
Concepts used: Arrays
In Python array, there are multiple ways to print the whole array with all the elements, but to print a specific range of elements from the array, we use Slice operation.
Slice operation is performed on array with the use of colon(:).
To print elements from beginning to a range use [:Index], to print elements from end use [:-Index], to print elements from specific Index till the end use [Index:], to print elements within a range, use [Start Index:End Index] and to print whole List with the use of slicing operation, use [:]. Further, to print whole array in reverse order, use [::-1].
A Python Array is a collection of common type of data structures
Arrays are same as lists. Only difference between them is list is only meant for row-wise operation but Arrays can do both row-wise and column-wise operations too.
The arrays are especially useful when you have to process the data dynamically.
Python arrays are much faster than list as it uses less memory.
We already know that image is just an 2D/3D array. First step is to create a set of arrays with our desired dimension/pixels using the following commands.
import numpy as np
Here, I will create my image with the dimension of 600x600 pixels and the name of the array is zero.
Its output will look like
Next step is to give/assign the color to that image since it is a plain black image.
It is done by using the concept of arrays and color codes which are available in internet. You can refer this color code by using below link.
One thing you should note, the color code followed in arrays is BGR not RGB.
The code and its outputs are given below.
The Output is
Left side image is our generated image and right side image is the master-piece developed in Paint.
Again we use the concept of Array slicing to crop and swap the image. You can refer to this in below code.
#cropping and replace(swap) the squares
Yes, Finally We successfully swapped the boxes.
You may notice this option in your Image editor in your phone. Yes, Now we are going to create our own Image Collager using concatenate function in Python.
There are two types of Concatenation namely Horizontal concatenate and Vertical Concatenate.
We are going to create an another 3D array and concatenate them horizontally with that existing array/image.
The code is listed below.
#Creating another same array(image) for horizontal concatenating
```#Horizontal concatenating axis=1
After concatenation done, We can see our output using cv2 library in Python by using below code.
waitKey is used to hold the operation and when we press enter, destroyAllWindows will destroy the process and process will shut down.
The output of Concatenation is shown in below figure.
Finally we done Concatenation too.
Thank you all for your reads. Stay tuned for my upcoming more interesting articles.