Image Processing Using Python3

Gaurav Tank
4 min readJun 9, 2021

--

In this blog, I am going to show you how we can create an image, crop it, swap some portions of the image and create a collage using python language.

Part1: Creating an Image using Python

For this, we need to import a library called OpenCV. If you don't have this library pre-installed you can install it using pip install opencv-python

and import is using import cv2

Now, to make this process simple I have created a function for creating an image, create()

def create():
photo= np.zeros((500,500,3))
nphoto = cv2.circle(photo,(100,100),50,(255,0,0),5)
nphoto = cv2.circle(photo,(150,150),50,(0,255,0),5)
nphoto = cv2.circle(photo,(200,200),50,(0,0,255),5)
nphoto = cv2.circle(photo,(250,250),50,(0,255,205),5)
nphoto = cv2.circle(photo,(300,300),50,(145,255,0),5)
show(photo)
create()

Here, in the photo variable, I have created a blank image of size 500 * 500 pixels.

With the help of cv2.circle() function I have created circles at the positions (100, 100), (150, 150), (200, 200), (250, 250), (300, 300), resp. And we can call the function create()

This will create an image as shown in the figure.

Part2: Cropping the Image using Python

For this, we need to load an image in our python code. We can load it using the OpenCV library by using this function.

import cv2
photo= cv2.imread('<<image_name>>')

These lines will store our image in the variable called photo. Here, I have also created a function for cropping the image. In fact, two images.

def crop(img1, img2):
photo1= cv2.imread(img1)
photo2= cv2.imread(img2)
c1= photo1[150:500, 50:500]
c2= photo2[150:500, 50:500]
show(c1)
show(c2)

This function will first load two images in our code then crop a specific portion of them. Here, I have cropped 150 to 500 rows [150: 500] and 50 to 500 columns [50: 500] and displayed the images through the show function.

def show(img):
cv2.imshow('photo', img)
cv2.waitKey()
cv2.destroyAllWindows()

This function takes only the image name as the argument and will show the image. Earlier, we have cropped the two images and stored them in c1 and c2 variables respectively then passed them as the arguments in the show function which will display the cropped image on the screen.

Cropped Images

Part3: Swapping the Portion of Images

For this, we need to load two images, crop them and then swap them. I have already shown you how to load the images and crop them using python. Now in the function first we are going to load the images, crop them and then swap the cropped part.

def swap(img1, img2):
photo1= cv2.imread(img1)
photo2= cv2.imread(img2)
photo3= cv2.imread(img1)
c1= photo3[150:500, 50:500]
c2= photo2[150:500, 50:500]
photo1[150:500, 50:500]= c2
photo2[150:500, 50:500]= c1
show(photo1)
show(photo2)

We can call the function by

swap(<<img1>>, <<img2>>)

Image Swapping

Part4: Collage Using Python

For making the collage we are going to use the cropped images as they both have the same dimensions. Now we are going to use hstack function of the NumPy module to join the images horizontally and vertically. The function for same is given below:

def collage(img1, img2):
photo1= cv2.imread(img1)
photo2= cv2.imread(img2)
c1= photo1[150:500, 50:500]
c2= photo2[150:500, 50:500]
horizontal= numpy.hstack((c1, c2))
vertical= numpy.vstack((c1, c2))
show(horizontal)
show(vertical)

Here, this function will require two arguments; names of the images then it will read the images using the cv2.imread function then crop the images for making them of the same dimensions then using the numpy’s hstack and vstack function we attach them breadthwise and lengthwise respectively.

It will generate two images as shown below.

NumPy.hstack
numpy.vstack

It is just the starting, there is much more we can do with the cv2 module in python like:

  • Automated inspection and surveillance.
  • Robot and driver-less car navigation and control.
  • Medical image analysis.
  • Video/image search and retrieval.
  • Movies — 3D structure from motion.
  • Interactive art installations.
  • Street view image stitching.

Hope you find this blog informative.

Thanks for reading!!!

--

--