Skip to content

dinesh4444/rembg-library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Removing Background from Images using rembg and OpenCV

This repository contains a Python script that demonstrates how to use the rembg library along with OpenCV to remove the background from an image and display the result. The script provides a graphical user interface (GUI) for selecting a region of interest (ROI) in an image and then applies background removal to the selected area.

Installation

Before running the script, you'll need to install the necessary libraries. You can install them using the following commands:

bash pip install opencv-python pip install numpy pip install rembg

About the 'rembg' Library

The 'rembg' library is a powerful tool designed for removing image backgrounds quickly and accurately. It utilizes deep learning techniques to achieve high-quality background removal without the need for complex manual adjustments. 'rembg' works particularly well for images with well-defined foreground objects and clear color contrasts between the object and the background.

Use Case

The primary use case for the rembg library is in scenarios where you want to isolate a subject or object from its background in images. Some common applications include:

  • E-commerce: Automatically remove backgrounds from product images to create clean, professional-looking listings.

  • Graphic Design: Easily create designs with transparent backgrounds for logos, icons, and other graphical elements.

  • Photography: Prepare images for creative projects by separating subjects from their surroundings.

  • Marketing Materials: Generate visually appealing marketing materials by placing subjects on different backgrounds.

  • Art and Creativity: Use background removal as a creative tool to merge different elements and experiment with compositions.

Usage

  1. Clone this repository to your local machine or download the script 'main.py' directly.

  2. Make sure you have an input image ('3.jpg' in the provided code). If you want to use a different image, make sure to update the 'input_image_path' variable in the script accordingly.

  3. Run the script using the following command: python main.py

  4. The script will open a GUI window displaying the input image. Follow these steps:

  • Resize the image for better visualization by adjusting the 'display_width' variable in the script.

  • Draw a rectangle over the object of interest in the image using the mouse. Press any key to continue after drawing the rectangle.

  • The script will then perform background removal on the selected ROI using the 'rembg' library.

  • The object with its background removed will be overlaid on the original image.

  • Press the 'c' key to clear the outline and start over, or press the 'q' key to quit the script.

Notes

  • The script uses the 'draw_outline' function to draw a green outline connecting the points of the selected ROI.

  • The 'resize_image' function is used to resize the image for better GUI display.

  • The 'rembg' library is employed to remove the background from the selected ROI.

  • Contours (outline points) are detected in the background-removed image, and the largest contour is drawn as an outline over the ROI.

  • The script provides an interactive user experience through the GUI, allowing you to experiment with background removal and object outlining.

  • Please feel free to modify and adapt the script according to your needs. Enjoy experimenting with background removal using the rembg library and OpenCV!

Releases

No releases published

Packages

No packages published

Languages