avatarArjun Gullbadhar

Summary

The undefined website provides a tutorial on using the Python library Rembg for AI-powered background removal from images, with options for both code-based and web app-based usage.

Abstract

The article introduces Rembg, a Python library designed to simplify the process of removing backgrounds from images using artificial intelligence. It emphasizes the rapid development of AI in various fields, including computer vision and natural language processing, and demonstrates how these advancements can be applied to image editing. The tutorial guides readers through installing Rembg via Google Colab, downloading an image, and executing the background removal process. It also offers a web application alternative for users who prefer not to engage with the code directly. The article concludes by encouraging readers to engage with the content and the AI community on Medium, suggesting a subscription to the platform for continued learning and support of independent writers in the AI field.

Opinions

  • The author expresses enthusiasm about the capabilities of AI in the context of image background removal, suggesting it as a replacement for traditional green screen methods.
  • There is a strong recommendation to use Google Colab for running the code, highlighting its convenience for users who do not wish to install software locally.
  • The author encourages hands-on interaction with the code to fully understand the process, while also providing a user-friendly web app for those who are less code-savvy.
  • The article promotes the idea of staying informed about AI advancements by subscribing to Medium, implying that such a subscription is a valuable investment for those interested in AI and machine learning.
  • The author values community feedback and invites readers to engage by liking, tipping, commenting, or reaching out with questions or suggestions.

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?

Input and Output Image side-by-side

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.

Rembg Repository

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.)

Google Colab Image

(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.

Installation of the library

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.

Input Image

Here is the rembg Output for the image

Output Image

Voilaaa!! and here you have it, a python library that removes background from your images.

Here are a few more examples.

Input Image
Output Image
Input Image
Output Image
Input Image
Output Image

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

Huggingface webapp

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

Machine Learning
Python
Programming
Artificial Intelligence
Deep Learning
Recommended from ReadMedium