The world’s leading publication for data science, AI, and ML professionals.

Easy Background Editing with Python AI

Remove the background and replace it to enhance your visual image with a few lines of Python code

Photo by Mark Harpur on Unsplash
Photo by Mark Harpur on Unsplash

You have a picture of yourself that you really like, but you can notice that the background in the surrounding image is creating a dampening effect on the image. You feel that if the background did not exist or was slightly altered by incorporating changes into the particular image by retaining your picture, it would result in a high-quality image portrait of yourself.

Some of the many ways to perform the following would include paying a decently high amount for purchasing software or paying a professional to complete the project. A few notable free resources might be available too, but their credibility or efficiency might be questionable with certain limitations.

Rather than dealing with the hassle of handling these issues, wouldn’t it be cool to just design your own signature method to manage the following task?

With the help of Python, the following project can be performed with ease. With a better understanding of computer vision and Artificial Intelligence, it is possible to significantly increase the performance and quality of the background removal from the images. In this article, our primary objective is to remove unnecessary backgrounds and retain the desired image.

For beginners to computer vision projects, I highly recommend one of my previous articles which is a detailed guide to mastering the basics of the Open CV computer vision library. The link to the following is provided below for convenient access.

OpenCV: Complete Beginners Guide To Master the Basics Of Computer Vision With Code!


Removal of background with a few lines of Python code:

Photo by Cristian Castillo on Unsplash
Photo by Cristian Castillo on Unsplash

In this section of the article, we will focus on removing the background of a particular image with Python. For this particular project, the version requirements are higher than Python 3.7 and lesser than Python 3.11 currently. The library that we will utilize for removing the background does not function with version 3.7 or lower.

As per my testing, the rembg library did not work with the Python 3.7 version as the appropriate scipy module versions could not be downloaded. However, with Python 3.9, I had absolutely no issues running the programs in this article after the following pip install command.

pip install rembg

The rembg library is developed with the help of deep neural networks, primarily utilizing the u2net architecture for handling the task of background removal. There are several modifications of the architecture and many tested methods to provide optimal results. The rembg library also grants access to a GPU installation for faster processing. For further information and understanding of the following library, I highly recommend checking the official GitHub repository from the following link.

Now that we have a brief idea of the library that we will utilize for the background removal project, it is time to import the two essential libraries for this project. As previously stated, the rembg library is our first preference to make the following task trivial and straightforward. More advanced developers can create their own neural networks for the project.

The other significant library that we will use is the pillow library, which is one of the best libraries for handling tasks related to images. This library is often installed in Python by default. If not, it can be installed with a simple pip installation command. The necessary libraries are mentioned in the code snippet below.

# Importing the necessary libraries
from rembg import remove
from PIL import Image

Once the required libraries are imported, the next step is to mention the input path of the image for which the background must be removed. The other parameter we will mention is the output path in the working directory titled "Image.png" for storing the transparent image without the background. The readers can choose any other file names and formats for the same.

# Specifying the working paths
input_path = "Dog.jpg"
out_path = "Image.png"

For the final background removal process, we will open the input image with the specified input path using the pillow library. We will use the remove functionality of the rembg library to then eliminate the overall background of the corresponding image with just the dog, which is the primary entity of focus to remain in the image.

The transparent background with the image of the dog is then saved in the working directory. The code snippet for performing the following action is provided below.

# Removing the background and saving the image
input = Image.open(input_path)
remove_bgnd = remove(input)
remove_bgnd.save(out_path)

After the successful compilation of the above program, the following transparent image of the dog is generated.

Image by Author
Image by Author

Once we have the transparent image of the dog, we can proceed to the upcoming section of the article to change and apply a new background accordingly. The readers can choose any desirable background as per their choice as long as the images are of the same size. Let us explore this topic further in the next section!


Changing the background:

Photo by Patrick Tomasso on Unsplash
Photo by Patrick Tomasso on Unsplash

Once we are finished with the removal of the background, more curious developers might wonder what to do with the previously acquired transparent image. For this next step, we will follow a similar and relatively easy process. We will combine the transparent dog image with the above background. Note that any selected background can be used for this task. The only library requirement that we will utilize for this task is the pillow library for operating on the transparent image and the background picture shown in the above image.

# Import the pillow library
from PIL import Image

In the next step, we will open both the images that are required for this background editing project. We will first open the transparent dog image, followed by the new background image that will be placed. The code snippet to open the following images is provided below.

# Opening the desired images
img1 = Image.open("Image.png")
img2 = Image.open("New_Bgnd.jpg")

Once we open both images, it is essential to ensure that all the images are of the same size. The following operation of pasting the background onto the transparent image of the dog will not work if the sizes of the two pictures do not match each other. Hence, it is a necessary step to verify the size of the images and appropriately perform any resizing if required.

For the viewers following this guide step-by-step, if you have clicked on the provided images and downloaded them onto your working directory, you will notice that all the images are of the size 880 x 557. If the size of the images does not match, feel free to resize them accordingly. The image sizes can be verified as shown below.

# Verifying image sizes
print(img1.size)
print(img2.size)

Finally, we will paste the background image onto the transparent image by using the paste command of the pillow library. The first parameter represents the first image (The dog with the transparent background), the next attribute is the starting coordinates, and finally, we have the critical parameter, which indicates a mask of the respective image.

The alpha channel is used as the mask which allows the user to merge the background with the transparent image of the dog. We can save the newly created image in our working directory. The code for performing the following action is provided in the code snippet below.

# Effectively changing the background
img2.paste(img1, (0, 0), img1)
img2.show()
img2.save("New_Dog.png")

Once we have completed the following project, we should be able to generate the following image.

Image by Author
Image by Author

With this two-step process, we have successfully removed the previous background while extracting the desired entity and posting the extracted dog with the transparent background into the new background. For more advanced variations of this guide, I recommend trying to fulfill these two tasks in a single project. The following guide can be transitioned into videos and real-time projects for background replacements, as in the case of a green screen to change backgrounds, as per the user’s desire.


Conclusion:

Photo by Boxed Water Is Better on Unsplash
Photo by Boxed Water Is Better on Unsplash

"In photography there is a reality so subtle that it becomes more real than reality." Alfred Stieglitz

Having an amazing picture of yourself while disliking the background or wanting to view yourself in a different atmosphere or region can sometimes be an issue. It can be tedious to use expensive software to edit the background manually. The results from other free background removal options might not produce the best outcomes either. However, with AI and Python, this project can be completed within a few lines of simple code.

In this article, we understood how to remove, edit, and replace your current background with your photo or the desired image of significance with the help of Python AI and a few lines of code. We make use of the pillow and rembg libraries to effectively remove the outlier background from the original image. Once the background is filtered, we can add a new desired background by using the original image and its respective mask.

To make additional improvements to the project, I highly recommend expanding this project from images to videos and even real-time operations. For highly advanced learning, I suggest an attempt to learn and construct deep neural networks for performing the background removal task from scratch.

If you want to get notified about my articles as soon as they go up, check out the following link to subscribe for email recommendations. If you wish to support other authors and me, then subscribe to the below link.

Join Medium with my referral link – Bharath K

If you have any queries related to the various points stated in this article, then feel free to let me know in the comments below. I will try to get back to you with a response as soon as possible.

Check out some of my other articles in relation to the topic covered in this piece that you might also enjoy reading!

Advanced GUI interface with Python

The Ultimate Replacements to Jupyter Notebooks

7 Best Research Papers To Read To Get Started With Deep Learning Projects

Thank you all for sticking on till the end. I hope all of you enjoyed reading the article. Wish you all a wonderful day!


Related Articles