Skip to main content

How to remove background from an image using python


The removal of background from an image is the mostly used technique in areas like photo editing, images processing, removing disturbing or noisy backgrounds from the images before showing and products image on shopping websites, preprocessing the data for creating models, this helps to make your images focusing on the actual subject in the image instead of focusing on the background noise and editing with other images, in tasks like object detection, to focus on the main product, to draw attention to products etc.


Here in this article we will learn how to remove background from images using Python and will specifically focus on use cases of this background removal technique. For this process we will be using Python libraries like rembg (Short for remove background), PIL (Short for Pillow library of python).
You can install this libraries using:

!pip install rembg
!pip install pillow

These image processing libraries adds image processing capabilities to your python interpreter so that we can deal with images. Pillow is a very powerful image processing library. If you got into any trouble while pillow installation you can visit the official documentation of pillow.

Now lets dive into the code for actual work, the code is very simple and with 3-4 lines we can remove background from any images. 

Importing the necessary libraries as mentioned above:

Now mention the input and output path for you image based on which kind of storage you are using:

Now pass the input image path of your image to the Image function of the pillow library:

Now using the remove function of rembg package we can remove the background of the input image using the code:

To save the output image you can simply do:

While saving the image as output, one important point to remember is that do not try to save the image as .jpg format as it cannot write mode as RGBA JPEG format. So try to save the image with extension as portable network graphics or .png fomat.

If you found this blog helpful please do share it with your friends and colleagues. Thank you !

 





Comments

Popular posts from this blog

Scrape PDF's using python

Hi, guys welcome to this blog post, i hope you guys are doing well. In this post i will discuss about how to scrape any specific text data or tables from PDF's and what kind of problems one can face while scrapping the PDF data.  The data trapped inside PDF are unstructured data and they can come from different sources like manually typed or system generated and depending on the source we have classified the PDF's into two categories  Simple or readable PDF's. Complex or scanned PDF's. Simple or readable PDF's: Simple PDF's can be of system generated or can come from data entry related sources and generally such kind of PDF's are less complicated and any kind of data can be easily extracted from such kind of PDFs.  Complex or scanned PDF's: On the other hand complex PDFs  or scanned PDFs are may come from system generated sources and generally are in scanned format and it is very difficult to handle the scanned PDFs and extracting data from it because so...

What is IoT (Internet Of Things)...???

This world has changed a lot, since the very beginning. Humans has made an significant development on this earth as compared to the other species. But it is not the thing that makes us so special on this earth, what makes us so special is the power of thinking that we have bestowed with. This is the thing that makes us so special on this planet earth. But are we humans really deserves this level of intelligence, although we have it naturally no doubt !!! For this... If we go back to the history and start digging our culture. how we survived from the great calamities, how we changed the world, how we developed our society, all the answers lies in it....!!! Biologically if we see every species on this is so well designed is to dominate the other species by making them on the top one but natures engineering is so perfect that it created a food chain, this food chain works so well that it maintains a perfect relationship between the apex ones and those species are at the bottom of the ...

All about data analysis and which programming language to choose to perform data analysis?

  What is data analysis ? Data analysis is the process of exploring, cleansing, transforming and modelling data in order to derive useful insight, supporting decision. Tools available for it ! There are two kinds of tools used in order to carry out data analysis: 1) Auto managed closed tools: These are the tools whose source code is not available, that is these are not open source. If you want to use these tools then you have to pay for them. Also, as these tools are not open source, if you want to learn these tools then you have to follow their documentation site. Though some auto managed tools have their free versions available.  Pros & Cons: Closed Source Expensive They are limited  Easy to learn Example: Tableau, Qlik View, Excel (Paid Version), Power BI (Paid Version), Zoho Analytics, SAS 2) Programming Languages: Then there are suitable programming languages which can derive the same result like auto managed closed tools.  Pros & Cons: These are open so...