Change the Background of Any Image

Change the Background of AnyImage

Image segmentation 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.

Image Tuning: It is the change in the background of an image through image segmentation. The key role of image segmentation is to remove the objects segmented from the image and place them in the new background created. This is done by producing a mask for the image and combining it with the modified background. We make use of model trained on pascalvoc dataset. The model supports 20 common object categories, which means you can change the background of these objects in images. By use of this method the photo background change easily.

The model supports the following objects listed below;

Background effects supported are:

1 Changing the background of an image with a picture

2 Assigning a distinct color to the background of an image.

3 Blurring the background of an image

4 Grayscaling the background of an image

Install PixelLib and its dependencies:

Install Tensorflow with:(PixelLib supports tensorflow 2.0 and above)

  • pip3 install tensorflow

Install PixelLib with

  • pip3 install pixellib

If installed, upgrade to the latest version using:

  • pip3 install pixellib — upgrade

Change the background of an image with a picture

PixelLib makes it possible to change the background of any image with a picture with just 5 lines of code.

sample.jpg

We want to change the background of the image above with the image provided below.

ALSO READ  A Look At A Sublimation Printing Machine

background.jpg

Code to change the background of an image with a picture

We imported pixellib, and from pixellib, we imported in the class alter_bg. We created an instance of the class.

We called the function change_bg_img that handled changing the background of the image with a picture.

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.

output Image

We are able to use PixelLib to perform excellent foreground and background subtraction through image segmentation.

Code to Obtain output array of the changed image array

For specialized uses, you can easily obtain the array of the changed image with the modified code below.

Assign a distinct color to the background of an image

You can assign a distinct color to the background of your image, just the way you are able to change the background of an image with a picture. This is also possible with five lines of code.

Code to assign a distinct color to the background of an image

It is very similar to the code used above for changing the background of an image with a picture. The only difference is that we replaced the function change_bg_img to color_bg, the function that handled color change.

The function color_bg takes the parameter colors and we provide the RGB value of the color we want to use. We want the image to have a green background and the color’s RGB value is set to green which is (0, 128, 0).

green background

Note:You can assign any color to the background of your image by providing the RGB value of the color.

ALSO READ  Top Hidden Features of Firefox Browser

We want to change the background of the image to white and set color’s RGB value to white which is (255,255,255).

white background

The same image with a white background.

Code to Obtain output array of the colored image

For specialized uses, you can easily obtain the array of the changed image with the modified code below.

Grayscale the background of an image

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

Code to grayscale the background of an image

It is still the same code except we called the function gray_bg to grayscale the background of the image.

output image

Note:The background of the image would be altered and the objects present would maintain their original quality.

Code to Obtain output array of the grayed image

Blur Image Background

You can apply the effect of blurring the background of an image, and it is possible to control how blur the background will be.

sample2.jpg

We called the function blur_bg to blur the background of the image, and set the blurred effect to be low. There are three parameters that determine the degree to which the background is blurred.

low: When it is set to true, the background is blurred slightly.

moderate: When it is set to true, the background is moderately blurred.

extreme: When it is set to true, the background is deeply blurred.

The image is blurred with a low effect.

We want to moderately blur the background of the image, and we set moderate to true.

The image’s background is blurred with a moderate effect.

ALSO READ  Laminate Cookie Boxes No Smug To Make It More Useful

We want to deeply blurred the background of the image, and we set extreme to true.

About Storify Go (Admin)

Hello! My name is Mr. Robert James. I am a content writer & full-time professional Web Designer and Developer specially WORDPRESS with vast experience. I started my graduation in 2014 and graduated in 2018. I'm a professional article and blog writer, has written dozens of content on different topics and worked with professionals all over the globe. My passion for exploring technology and gathering unique information for the benefit of others has led me to pursue a career in news reporting. I take pride in providing timely coverage of the latest news across Pakistan as a personal hobby and professional responsibility."

View all posts by Storify Go (Admin)