NAV
shell python php javascript

به اسناد BackgroundRemover.app API خوش آمدید

برای دریافت کلید توسعه دهنده لطفاً به پورتال توسعه دهنده

Authorization: <api_key>

حذف پس زمینه تصویر

حذف پس زمینه تصویر

import requests
import time
import shutil
import json

headers = {'Authorization': 'f134382194a844c8bb589af58ef283e9'}
file_list = ['testfiles/blah.jpeg', 'testfiles/blah.jpeg', 'testfiles/blah.jpeg']
params = {
    'lang': 'en',
    'convert_to': 'image-backgroundremover',
    'ocr': False
}

api_url = 'https://api.backgroundremover.app/v1/convert/'
results_url = 'https://api.backgroundremover.app/v1/results/'


def download_file(url, local_filename):
    with requests.get("https://api.backgroundremover.app/%s" % url, stream=True) as r:
        with open(local_filename, 'wb') as f:
            shutil.copyfileobj(r.raw, f)
    return local_filename


def convert_files(api_url, params, headers):
    files = [eval(f'("files", open("{file}", "rb"))') for file in file_list]
    print(files)
    r = requests.post(
        url=api_url,
        files=files,
        data=params,
        headers=headers
    )
    return r.json()


def get_results(params):
    if params.get('error'):
        return params.get('error')
    r = requests.post(
        url=results_url,
        data=params
    )
    data = r.json()
    finished = data.get('finished')
    while not finished:
        if int(data.get('queue_count')) > 0:
            print('queue: %s' % data.get('queue_count'))
        time.sleep(5)
        results = get_results(params)
        print(results)
        results = json.dumps(results)
        if results:
            break
    if finished:
        print(data.get('files'))
        for f in data.get('files'):
            print(f.get('url'))
            download_file("%s" % f.get('url'), "%s" % f.get('filename'))
        return {"finished": "files downloaded"}
    return r.json()


get_results(convert_files(api_url, params, headers))

Request:
curl -F "lang=en" -F "convert_to=image-backgroundremover" -F "files=@testfiles/blah.jpeg" -F "files=@testfiles/blah.jpeg" -F "files=@testfiles/blah.jpeg" -H "Authorization: <api_key>" https://api.backgroundremover.app/v1/convert/
Response:
{uuid: <conversion_uuid>}


Request:
curl -F "uuid=<conversion_uuid>" -H "Authorization: <api_key>" https://api.backgroundremover.app/v1/results/
Response:
{"files": [{"url": <url>, filename: <filename>}], "failed": [], "finished": true, "queue_count": 0, "errors": []}





<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$headers = array("Authorization: <api_key>");
$file_list = ['testfiles/blah.jpeg', 'testfiles/blah.jpeg', 'testfiles/blah.jpeg'];
$api_url = 'https://api.backgroundremover.app/v1/convert/';
$results_url = 'https://api.backgroundremover.app/v1/results/';
$post_data = [];

function download_file($url, $filename){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSLVERSION, 3);
    $data = curl_exec($curl);
    $error = curl_error($curl);
    curl_close ($curl);
    $file = fopen("./testfiles/$filename", "w+");
    fputs($file, $data);
    fclose($file);
}

function convert_files($file_list, $headers, $api_url) {
    $post_data['lang'] = 'en';
    $post_data['convert_to'] = 'image-backgroundremover';

    foreach ($file_list as $index => $file) {
        $post_data['file[' . $index . ']'] = curl_file_create(
            realpath($file),
            mime_content_type($file),
            basename($file)
        );
    }

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $api_url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $content = curl_exec($curl);
    curl_close($curl);
    print($content);

    return $content;
}

function get_results($params, $api_url, $headers) {
    $error = json_decode($params);
    print_r($params);
    print_r($error);

    if ($error->error) {
        print_r($error->error);
        return;
    }

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $api_url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $content = json_decode(curl_exec($curl));
    curl_close($curl);

    while (!$content->finished) {
        if (intval($content->queue_count) > 0) {
            print_r("queue: $content->queue_count");
        }

        sleep(5);
        $results = get_results($params, $api_url, $headers);
    }

    foreach ($content->files as $f) {
        download_file($f->url, $f->filename);
    }
}

get_results(convert_files($file_list, $headers, $api_url), $api_url, $headers);
?>


const request = require('request');
const fs = require('fs');
const { initParams } = require('request');

let file_list = ['testfiles/blah.jpeg']
const api_url = 'https://api.backgroundremover.app/v1/convert/'
const results_url = 'https://api.backgroundremover.app/v1/results/'

function convertFiles(file_list) {
    let formData = {
        'lang': 'en',
        'convert_to': 'image-backgroundremover'
    };

    for (var i = 0; i < file_list.length; i ++) {
        formData['files'] = fs.createReadStream(file_list[i]);
    }

    request({
        url: api_url,
        method: 'post',
        formData: formData,
        headers: {
            'Authorization': '<api_key>',
            "Content-Type": "multipart/form-data",
        }
    }, function(e, r, body) {
        getResults(JSON.parse(body));
    });
}

function getResults(data) {
    if (data.error) {
        return data.error;
    }

    request({
        url: results_url,
        method: 'post',
        formData: data
    }, function(e, r, body) {
        response = JSON.parse(body);

        if (!response.finished) {
            setTimeout(
                function() {
                    getResults(data);
                }, 1000
            );
        }

        console.log(response);
    })
}

convertFiles(file_list);

بر خواهد گشت

/path/to/local/file_processed.png

برای ایجاد چندگانه کافی است فایل های بیشتری را در لیست خود قرار دهید

حذف پس زمینه ویدیو

حذف پس زمینه ویدیو

import requests
import time
import shutil
import json

headers = {'Authorization': 'f134382194a844c8bb589af58ef283e9'}
file_list = ['testfiles/blah.mp4', 'testfiles/blah.mp4', 'testfiles/blah.mp4']
params = {
    'lang': 'en',
    'convert_to': 'video-backgroundremover',
    'ocr': False
}

api_url = 'https://api.backgroundremover.app/v1/convert/'
results_url = 'https://api.backgroundremover.app/v1/results/'


def download_file(url, local_filename):
    with requests.get("https://api.backgroundremover.app/%s" % url, stream=True) as r:
        with open(local_filename, 'wb') as f:
            shutil.copyfileobj(r.raw, f)
    return local_filename


def convert_files(api_url, params, headers):
    files = [eval(f'("files", open("{file}", "rb"))') for file in file_list]
    print(files)
    r = requests.post(
        url=api_url,
        files=files,
        data=params,
        headers=headers
    )
    return r.json()


def get_results(params):
    if params.get('error'):
        return params.get('error')
    r = requests.post(
        url=results_url,
        data=params
    )
    data = r.json()
    finished = data.get('finished')
    while not finished:
        if int(data.get('queue_count')) > 0:
            print('queue: %s' % data.get('queue_count'))
        time.sleep(5)
        results = get_results(params)
        print(results)
        results = json.dumps(results)
        if results:
            break
    if finished:
        print(data.get('files'))
        for f in data.get('files'):
            print(f.get('url'))
            download_file("%s" % f.get('url'), "%s" % f.get('filename'))
        return {"finished": "files downloaded"}
    return r.json()


get_results(convert_files(api_url, params, headers))

Request:
curl -F "lang=en" -F "convert_to=video-backgroundremover" -F "files=@testfiles/blah.jpeg" -F "files=@testfiles/blah.jpeg" -F "files=@testfiles/blah.jpeg" -H "Authorization: <api_key>" https://api.backgroundremover.app/v1/convert/
Response:
{uuid: <conversion_uuid>}


Request:
curl -F "uuid=<conversion_uuid>" -H "Authorization: <api_key>" https://api.backgroundremover.app/v1/results/
Response:
{"files": [{"url": <url>, filename: <filename>}], "failed": [], "finished": true, "queue_count": 0, "errors": []}





<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$headers = array("Authorization: <api_key>");
$file_list = ['testfiles/blah.mp4', 'testfiles/blah.mp4', 'testfiles/blah.mp4'];
$api_url = 'https://api.backgroundremover.app/v1/convert/';
$results_url = 'https://api.backgroundremover.app/v1/results/';
$post_data = [];

function download_file($url, $filename){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSLVERSION, 3);
    $data = curl_exec($curl);
    $error = curl_error($curl);
    curl_close ($curl);
    $file = fopen("./testfiles/$filename", "w+");
    fputs($file, $data);
    fclose($file);
}

function convert_files($file_list, $headers, $api_url) {
    $post_data['lang'] = 'en';
    $post_data['convert_to'] = 'video-backgroundremover';

    foreach ($file_list as $index => $file) {
        $post_data['file[' . $index . ']'] = curl_file_create(
            realpath($file),
            mime_content_type($file),
            basename($file)
        );
    }

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $api_url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $content = curl_exec($curl);
    curl_close($curl);
    print($content);

    return $content;
}

function get_results($params, $api_url, $headers) {
    $error = json_decode($params);
    print_r($params);
    print_r($error);

    if ($error->error) {
        print_r($error->error);
        return;
    }

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $api_url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $content = json_decode(curl_exec($curl));
    curl_close($curl);

    while (!$content->finished) {
        if (intval($content->queue_count) > 0) {
            print_r("queue: $content->queue_count");
        }

        sleep(5);
        $results = get_results($params, $api_url, $headers);
    }

    foreach ($content->files as $f) {
        download_file($f->url, $f->filename);
    }
}

get_results(convert_files($file_list, $headers, $api_url), $api_url, $headers);
?>


const request = require('request');
const fs = require('fs');
const { initParams } = require('request');

let file_list = ['testfiles/blah.mp4']
const api_url = 'https://api.backgroundremover.app/v1/convert/'
const results_url = 'https://api.backgroundremover.app/v1/results/'

function convertFiles(file_list) {
    let formData = {
        'lang': 'en',
        'convert_to': 'video-backgroundremover'
    };

    for (var i = 0; i < file_list.length; i ++) {
        formData['files'] = fs.createReadStream(file_list[i]);
    }

    request({
        url: api_url,
        method: 'post',
        formData: formData,
        headers: {
            'Authorization': '<api_key>',
            "Content-Type": "multipart/form-data",
        }
    }, function(e, r, body) {
        getResults(JSON.parse(body));
    });
}

function getResults(data) {
    if (data.error) {
        return data.error;
    }

    request({
        url: results_url,
        method: 'post',
        formData: data
    }, function(e, r, body) {
        response = JSON.parse(body);

        if (!response.finished) {
            setTimeout(
                function() {
                    getResults(data);
                }, 1000
            );
        }

        console.log(response);
    })
}

convertFiles(file_list);

بر خواهد گشت

/path/to/local/file_processed.mov
/path/to/local/file_processed.gif

برای ایجاد چندگانه کافی است فایل های بیشتری را در لیست خود قرار دهید