Rembg

2.0.75 · active · verified Sat Apr 11

Rembg is an open-source Python library that uses deep learning models to automatically remove backgrounds from images. It provides a simple API and command-line interface, making it suitable for various applications such as e-commerce, graphic design, and automated content creation. The library is actively maintained, with frequent patch releases, and is currently at version 2.0.75.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `rembg` to remove the background from an image. It highlights the use of `new_session` for efficiency and assumes `PIL` for image handling. For a real scenario, replace the dummy image creation with loading from a file (e.g., `Image.open('input.png')`).

from PIL import Image
from rembg import remove, new_session

# Create a session to improve performance for multiple images
session = new_session()

# Example: Load an image from a dummy source or local path
# For a real application, replace this with actual image loading
# For demonstration, we'll create a blank image
# In a real scenario, you'd use Image.open('path/to/your/image.png')

try:
    # Simulate loading an image (replace with actual image path)
    input_image = Image.new('RGBA', (200, 200), (255, 0, 0, 255)) # A red square
    # Or, if you have an actual image file:
    # input_image = Image.open('path/to/your/image.png')

    # Remove the background
    output_image = remove(input_image, session=session)

    # Save the result
    # output_image.save('output.png')
    print("Background removed successfully (output not saved in this example).")
    print(f"Original image mode: {input_image.mode}, size: {input_image.size}")
    print(f"Output image mode: {output_image.mode}, size: {output_image.size}")
except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure you have an actual image file or mock image data for processing.")

view raw JSON →