Remove background from Images using Python and AI
Remove the background from images without spending hours on photo editing apps using AI. Bye, Bye Green Screen?
Artificial Intelligence(AI) has been developing at a very rapid speed, be it computer vision, NLP(Natural Language Processing), Speech Synthesis, Art creation… The list is endless. But can it remove backgrounds from your images?
Yes, It Can.
Introducing Rembg, A python library that allows you to remove backgrounds from images. It is very easy to install and use. You can use it from the command line as well as from python code. It is written in python and is available on GitHub at https://github.com/danielgatis/rembg It is available in CPU and GPU versions, so if you do not have a gpu you can still use this library.
I Will be using Google Colab to demonstrate the usage of this library.
(Google colab allows us to run code in the browser without installing anything locally.)
(if you are unfamiliar with colab or do not want to run the entire code, there is a link to a web app at the end of this article where you can upload your images directly and play around with the model. But I highly encourage you to play around with the code.)
Let’s Get Started.
Go to https://colab.research.google.com/ and Click on “New Notebook”. You will see a new colab notebook open. Click on the first cell and enter the following command and hit run. This will install the ‘rembg’ library and all the other required libraries.
!pip install rembg
You should see something like this.
This step will take around a minute to run.
The next step is to download the image from a given url so that we can use that as an input for rembg. The following code does this for you.
from google.colab import files as FILE
import os
import requests
url="https://raw.githubusercontent.com/danielgatis/rembg/master/examples/girl-2.jpg"
img_data = requests.get(url).content
with open('input.jpg', 'wb') as handler:
handler.write(img_data)
This will download the image to google colab. Now use the following command-line command to call rembg to remove background from content/input.jpg and store the output as /content/image1_output_bg.jpg.
!rembg i /content/input.jpg /content/image1_output_bg.jpg
Here is the input image we used.
Here is the rembg Output for the image
Voilaaa!! and here you have it, a python library that removes background from your images.
Here are a few more examples.
But how do we use our own images without running code? Here is the link which allows you to drag and drop without running any code. To use it, simply upload your image and click submit. An image may take some time to process as it runs on the CPU. https://huggingface.co/spaces/KenjieDec/RemBG
Here you have it, AI removing background from images.
Are you passionate about artificial intelligence and machine learning? Do you want to stay up-to-date on the latest advancements and insights in the field?
Then subscribing to Medium is a must. For just $5 a month, you’ll have access to a wealth of knowledge and insights from experts in the field.
Plus, your subscription helps support independent writers and researchers like myself. And with the rapid pace of change in the world of AI, staying informed is crucial. Don’t miss out — join Medium today and become a part of the AI revolution. It’s the best decision you’ll ever make. Subscribe to Medium today using this link.
Hey Guys,
I am pretty new to writing on Medium and would love to hear your feedback. If you like what I am writing about, don’t hesitate to put a thumb up or leave a tip. Please feel free to leave a comment if you have a question or recommendation. I read every message and try to answer as soon as I can.
Arjun Gullbadhar