DEV Community

Cover image for Create, Crop, Swap and Collate a BGR image using Python

Create, Crop, Swap and Collate a BGR image using Python

Sri Vishnuvardhan A
A Tech Enthusiast who loves to learn & integrate multiple technologies and keen to find the innovative solution of it to solve the existing challenges!!
Updated on ・4 min read

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

Aray Slicing

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].


credit: GeeksForGeeks


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.

Creating your own image

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 cv2
import numpy as np
size= 600,600,3
zero=np.zeros(size, dtype=np.uint8)

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.

zero[0:200,0:200]=[255,153,51] zero[0:200,200:400]=[0,0,0]

The Output is


Left side image is our generated image and right side image is the master-piece developed in Paint.

Crop and Swap the image

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

The output is
3 replace2

Yes, Finally We successfully swapped the boxes.

Collate the image

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
size= 600,600,3
Hconcate=np.zeros(size, dtype=np.uint8)

```#Horizontal concatenating axis=1

Vertical concatenating axis=0


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.

4 concate

Finally we done Concatenation too.

Thank you all for your reads. Stay tuned for my upcoming more interesting articles.

Discussion (0)