NAV
python

BackgroundRemover.app uses API keys to allow access to the API. You can register a new API key at our

By going hereDeveloper portal

Authorization: <api_key>

Images

Remove image background from single or multiple images

import requests

file_names = ["/path/to/file.jpg"]
api_key = '<YOUR_API_KEY>'

domain = 'https://image.backgroundremover.app'
api_version = 'api/v1/'
headers = {'Authorization': api_key}

def images():
    files = [('image', open(f, 'rb')) for f in file_names]
    r = requests.post("%s/%simage/" % (domain, api_version), files=files, headers=headers)
    return r.json()

def get_results(job_name, count, type):
    r = requests.post("%s/%sresults/" % (domain, api_version), data={'job_name': job_name, 'count': count, "type": type})
    return r.json()

def download_file(url):
    local_filename = url.split('/')[-1]
    with requests.get(url, stream=True) as r:
        r.raise_for_status()
        with open(local_filename, 'wb') as f:
            for chunk in r.iter_content(chunk_size=8192):
                f.write(chunk)

image = images()
if not image.get('error'):
    results = get_results(image['job_name'], image['count'], image['type'])
    while not results['finished'] or not results['error']:
        results = get_results(word['job_name'], word['count'], word['type'])
        results['finished'] = results['finished']
        time.sleep(5)
        if results['finished']:
            print(results['finished'])
            break
    for f in results['files']:
        file = download_file(f[0])
        print(file)

The above command returns something like this:

/path/to/local/file_processed.png

To do multiple images please simply make `file_names = ["/path/to/file.jpg"]` a list of more files, such as `file_names = ["/path/to/file.jpg", "/path/to/file2.jpg"]` This endpoint will remove the background from a list of `jpeg` and/or `png` images

Videos

Remove video backgrounds from single or multiple videos

import requests

file_names = ["/path/to/file.mp4"]
api_key = '<YOUR_API_KEY>'


domain = 'https://video.backgroundremover.app'
api_version = 'api/v1/'
headers = {'Authorization': api_key}
def videos():
    files = [('video', open(f, 'rb')) for f in file_names]
    r = requests.post("%s/%svideo/" % (domain, api_version), files=files, headers=headers)
    return r.json()

def get_results(job_name, count, type):
    r = requests.post("%s/%sresults/" % (domain, api_version), data={'job_name': job_name, 'count': count, "type": type})
    return r.json()

def download_file(url):
    local_filename = url.split('/')[-1]
    with requests.get(url, stream=True) as r:
        r.raise_for_status()
        with open(local_filename, 'wb') as f:
            for chunk in r.iter_content(chunk_size=8192):
                f.write(chunk)

image = videos()
if not image.get('error'):
    results = get_results(image['job_name'], image['count'], image['type'])
    while not results['finished'] or not results['error']:
        results = get_results(word['job_name'], word['count'], word['type'])
        results['finished'] = results['finished']
        time.sleep(5)
        if results['finished']:
            print(results['finished'])
            break
    for f in results['files']:
        file = download_file(f[0])
        print(file)

The above command returns something like this:

/path/to/local/file_processed.mov

To do multiple images please simply make `file_names = ["/path/to/file.mp4"]` a list of more files, such as `file_names = ["/path/to/file.mp4", "/path/to/file2.mp4"]` This endpoint will remove the background from a list of `mp4` and/or `gif` videos and return a mov.