DEV Community

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

Posted on • Updated on

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

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

list

credit: GeeksForGeeks

Array

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

image

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.
https://www.rapidtables.com/web/color/RGB_Color.html

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]
zero[0:200,400:600]=[0,255,255]
zero[200:400,0:200]=[204,0,204]
zero[200:400,200:400]=[0,255,128]
zero[200:400,400:600]=[0,0,255]
zero[400:600,0:200]=[0,102,204]
zero[400:600,200:400]=[51,153,255]
zero[400:600,400:600]=[0,153,0]

The Output is

1

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
zero[100:200,100:200]=[0,153,0]
zero[100:200,400:500]=[255,153,51]
zero[200:400,100:200]=[0,255,255]
zero[200:400,200:400]=[204,0,204]
zero[200:400,400:500]=[0,255,128]
zero[400:500,100:200]=[51,153,255]
zero[400:500,200:400]=[0,102,204]
zero[400:500,400:500]=[0,0,0]
zero[100:200,200:400]=[0,255,255]

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)
Hconcate[0:200,0:200]=[255,153,51]
Hconcate[0:200,200:400]=[0,0,0]
Hconcate[0:200,400:600]=[0,255,255]
Hconcate[200:400,0:200]=[204,0,204]
Hconcate[200:400,200:400]=[0,255,128]
Hconcate[200:400,400:600]=[0,0,255]
Hconcate[400:600,0:200]=[0,102,204]
Hconcate[400:600,200:400]=[51,153,255]
Hconcate[400:600,400:600]=[0,153,0]

```#Horizontal concatenating axis=1

Vertical concatenating axis=0

horizontal_concate=np.concatenate((zero,Hconcate),axis=1)```

After concatenation done, We can see our output using cv2 library in Python by using below code.

cap=cv2.imshow("photo",horizontal_concate)
cv2.waitKey()
cv2.destroyAllWindows()

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.

Top comments (0)