Image Processing Using Python3

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 =,(100,100),50,(255,0,0),5)
nphoto =,(150,150),50,(0,255,0),5)
nphoto =,(200,200),50,(0,0,255),5)
nphoto =,(250,250),50,(0,255,205),5)
nphoto =,(300,300),50,(145,255,0),5)

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

With the help of 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]

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)

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

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))

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.


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!!!




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The secret behind successful legacy modernization projects

Scaling Out Azure Functions With Event Hubs Effectively 2

Making a software factory from scratch: a true story (Part 1: the bases)

ScandiPWA Updates: July 3 (Issue #54)

There’s 🚫 place like 🏠 k3s — Meetup Notes

How to learn Machine Learning as an amateur? (2)

LDAP Authentication with Spring Boot

Google Cloud Data Catalog — Live Sync Your On-Prem Hive Server Metadata Changes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gaurav Tank

Gaurav Tank

More from Medium

Face Detection using OpenCV


Detect tip of tongue using OpenCV

Object detection using Darknet and YOLO