Background removal takes an existing image you provide and removes those parts of the image considered to be “background.”

Example Code of removing background from an image:

Python
import requests
import json
import os
import base64
import time
import io
import PIL.Image
from typing import Optional, Tuple


def _process_test(url):
    
    image = PIL.Image.open("headphones1.jpeg")

    # Create a BytesIO buffer to hold the image data
    image_buffer = io.BytesIO()
    image.save(image_buffer, format='JPEG')
    image_bytes = image_buffer.getvalue()
    encoded_image = base64.b64encode(image_bytes).decode('utf-8')

    OCTOAI_TOKEN = os.environ.get("OCTOAI_TOKEN")

    payload = {
        "init_image": encoded_image,
        "bgcolor":(255, 255, 255, 0)
    }
    headers = {
        "Authorization": f"Bearer {OCTOAI_TOKEN}",
        "Content-Type": "application/json",
        "X-OctoAI-Queue-Dispatch": "true"
    }

    response = requests.post(url, headers=headers, json=payload)

    if response.status_code != 200:
        print(response.text)
    print(response.json())

    img_info = response.json()["image_b64"]

    img_bytes = base64.b64decode(img_info)
    img = PIL.Image.open(io.BytesIO(img_bytes))

    if img.mode == 'RGBA':
     img = img.convert('RGB')

    img.save("result_image.png")

if __name__ == "__main__":
    _process_test("https://image.octoai.run/background-removal")