DEV Community

Cover image for How to Change Background of an image using PixelLib
Sunil Aleti
Sunil Aleti

Posted on

How to Change Background of an image using PixelLib

Before talking about "What is Pixellib and what does it do?", let's talk about Image Segmentation

Image Segmentation

It is the process of partitioning a digital image into multiple segments. The goal of segmentation is to simplify and/or change the representation of an image into something that is more meaningful and easier to analyze. Image segmentation is typically used to locate objects and boundaries (lines, curves, etc.) in images. It has a lot of amazing applications that solve different computer vision problems.

PixelLib is a library created to ensure easy integration of image segmentation in real-life applications. PixelLib now supports a feature known as image tuning.

What is Image tuning?

Nothing but changing the background of an image with a custom background or adding a distinct color to the background or converting a background to grayscale. And we make use of deeplabv3+ model trained on pascalvoc dataset. The model supports 20 common object categories, which means you can change the background of these objects in images.

The model supports the following objects:
person, bus, car, aeroplane, bicycle, motorbike, bird, boat, bottle, cat, chair, cow, dinning table, dog, horse, sheep, sofa, train, tv

We should install pixellib and its dependencies

  • pip3 install pixellib
  • pip3 install tensorflow

For demonstration purposes, let me select one of my pictures and also a custom image

20201115_012958_0000.png

Code to change the background of an image with a picture

Now we changing the background of the 1st image with the 2nd image

import pixellib
from pixellib.tune_bg import alter_bg

change_bg = alter_bg()
change_bg.load_pascalvoc_model("deeplabv3_xception_tf_dim_ordering_tf_kernels.h5")
change_bg.change_bg_img(f_image_path = "me.jpg",b_image_path = "bg.jpg", output_image_name="new_img.jpg")
Image(filename='new_img.jpg',width=300,height=350)
Enter fullscreen mode Exit fullscreen mode

We imported pixellib, and from pixellib, we imported in the class alter_bg

We loaded deeplabv3+ model.

The function takes the following parameters:

f_image_path: This is the foreground image, the image which background would be changed.

b_image_path: This is the image that will be used to change the background of the foreground image.

output_image_name: The new image with a changed background.

And this is the "new_img"

20201115_014635_0000.png

To blur the background of an image:

It is also possible to control how blur the background should be.

  1. low=True
  2. moderate=True
  3. extreme=True
change_bg.blur_bg("me.jpg",extreme=True, output_image_name="blur_img.jpg")
Enter fullscreen mode Exit fullscreen mode

20201115_013810_0000.png

To Grayscale the background of an image:

Grayscale the background of any image using the same lines of code with PixelLib.

change_bg.gray_bg("me.jpg",output_image_name="gray_img.jpg")
Enter fullscreen mode Exit fullscreen mode

20201115_014855_0000.png

You can also apply these background effects on videos too

Refer to these resources:

https://pixellib.readthedocs.io/en/latest/
https://github.com/ayoolaolafenwa/PixelLib

Hope it's useful

A ❤️ would be Awesome 😊

Top comments (7)

Collapse
 
ayoolaolafenwa profile image
Olafenwa Ayoola

Thank you very much for this wonderful article you published on how to change image background with PixelLib. I am the creator of PixelLib and If you have any question concerning this library you can reach to me.

Collapse
 
sunilaleti profile image
Sunil Aleti

Thanks and sure 😊

Collapse
 
theviren profile image
Virendra Chaudhary

Thank you so much for the post. I tried this. But I am not sure why I am getting following error. Any help to fix this issue is really appreciated.

File "change_background.py", line 3, in <module>
from pixellib.tune_bg import alter_bg
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pixellib/tune_bg/__init__.py", line 4, in <module>
from pixellib.semantic.deeplab import Deeplab_xcep_pascal
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pixellib/semantic/__init__.py", line 4, in <module>
from .deeplab import Deeplab_xcep_pascal
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pixellib/semantic/deeplab.py", line 15, in <module>
from tensorflow.python.keras.layers import BatchNormalization
ImportError: cannot import name 'BatchNormalization' from 'tensorflow.python.keras.layers' (/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/tensorflow/python/keras/layers/__init__.py)

Collapse
 
sunilaleti profile image
Sunil Aleti

Itseems you have some dependencies issue, try running in google colab

Collapse
 
divyagattani profile image
divyagattani

I am trying it in google colab even though i am getting the same error
can you help how to solve this issue ?

Collapse
 
sakshat682 profile image
Sakshat Jain

I was facing the same problem so I reduce the tensorflow version to 2.4.1 and also reduce the python version to 3.7 (pip install tensorflow==2.4.1)

Collapse
 
yuliyasheichenka profile image
YuliyaSheichenka

I tried to run this code for my purposes and I got the following error:
"""
/usr/local/lib/python3.8/dist-packages/h5py/_hl/files.py in make_fid(name, mode, userblock_size, fapl, fcpl, swmr)
171 if swmr and swmr_support:
172 flags |= h5f.ACC_SWMR_READ
--> 173 fid = h5f.open(name, flags, fapl=fapl)
174 elif mode == 'r+':
175 fid = h5f.open(name, h5f.ACC_RDWR, fapl=fapl)

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/h5f.pyx in h5py.h5f.open()

OSError: Unable to open file (unable to open file: name = 'deeplabv3_xception_tf_dim_ordering_tf_kernels.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)
"""
Any ideas on how to resolve it? It looks like the library cannot find the model it is supposed to use.