Background Tuning Of Images With DeepLab V3 Using Pixellib

Edit the picture background based on image segmentation with pre-trained deep learning model in python

If today, I say that you don’t need any software to edit your pictures and can edit by yourself with pre-trained deep learning model in python, how awesome would that be?

Let’s see how we can make it.

But before that one needs to understand what foreground and background are.


Sign up for your weekly dose of what's up in emerging technology.

Source: Pinterest

Required Deep Learning Tools

Now we will try to understand the dependency and how can one download the pre-trained model.

Download our Mobile App

To download the pre-trained model:

Open your browser and copy this URL there, press enter.

Dependency: Pixellib

Installation: pip install pixellib


#importing packages
import pixellib
from pixellib.tune_bg import alter_bg
from matplotlib import pyplot as plt
import numpy as np
from PIL import Image
from IPython.display import Image as img
from pylab import rcParams
rcParams['figure.figsize'] = 10, 10 #it increases the size of plot
change_bg = alter_bg() #object creation
#here alter_bg() is a class
print(dir(change_bg)) #the functions it consists of

Loading Pre-Trained DeepLab V3

Here, we will load the pre-trained deep learning model that is DeepLab V3 for our task of background tuning.

#loading pre trained model

Loading Images

Now, as we are ready with the pre-trained model for background tuning, we will load the main image and the image of the required background. 

Main image:




Background image:




Blur Background

First of all, we will blur the background of the main image.




image, segmentation, deep learning, picture, deepleab v3, background

Grey Background

In the next step, we will make the background grey.




image, segmentation, deep learning, picture, deepleab v3, background

Changing the background to a Solid Color

In this step, we will set the background of the main image to s solid colour.

change_bg.color_bg(file_name, colors = (225, 225, 225), output_image_name = "colored_bg.jpg")



image, segmentation, deep learning, picture, deepleab v3, background

Changing the Background

Finally, we will change the background of the main image.

change_bg.change_bg_img(f_image_path = file_name,b_image_path = bg_file, output_image_name = "new_img.jpg")



image, segmentation, deep learning, picture, deepleab v3, background


As we could see above, we were able to tune the background of the image very effectively. It required a very effort and could be achieved in a very few easy steps even when we were using deep learning. So using the pre-trained deep learning models yield effective results with less coding efforts. 

Hope you liked the article. Stay tuned for more.

You can follow me at the handles mentioned. 
The complete code of the above implementation is available at the AIM’s GitHub repository. Please visit this link to find the notebook of this code.

More Great AIM Stories

Bhavishya Pandit
Understanding and building fathomable approaches to problem statements is what I like the most. I love talking about conversations whose main plot is machine learning, computer vision, deep learning, data analysis and visualization. Apart from them, my interest also lies in listening to business podcasts, use cases and reading self help books.

AIM Upcoming Events

Conference, in-person (Bangalore)
Rising 2023 | Women in Tech Conference
16-17th Mar, 2023

Early Bird Passes expire on 10th Feb

Conference, in-person (Bangalore)
Data Engineering Summit (DES) 2023
27-28th Apr, 2023

3 Ways to Join our Community

Telegram group

Discover special offers, top stories, upcoming events, and more.

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Subscribe to our Daily newsletter

Get our daily awesome stories & videos in your inbox