Remove Image Background Programmatically via the API

Except the online version, PhotoScissors offers the API for removing image backgrounds with just few lines of code.

How to integrate background removal capabilities into any app or website:

  • Step 1: Get your API Key.
  • Step 2: Use the following code samples to get started quickly

cURL

curl -H 'X-API-Key: INSERT_YOUR_API_KEY_HERE' \
	-F '[email protected]/path/to/file.jpg' \
	-F 'mode=image' \
	-F 'format=jpg' \
	-F 'background_color=#FFFFFF' \
	-f https://api.photoscissors.com/v1/change-background \
	-o result.jpg

Node.js

// Requires "request" to be installed (see https://www.npmjs.com/package/request)
var request = require('request');
var fs = require('fs');

request.post({
	url: 'https://api.photoscissors.com/v1/change-background',
	formData: {
		image: fs.createReadStream('/path/to/file.jpg'),
		mode: 'image',
		format: 'jpg',
		backround_color: '#FFFFFF'
	},
	headers: {
		'X-Api-Key': 'INSERT_YOUR_API_KEY_HERE'
	},
	encoding: null
}, function(error, response, body) {
	if(error) 
		return console.error('Request failed:', error);
	if(response.statusCode != 200) 
		return console.error('Error:', response.statusCode, body.toString('utf8'));
	fs.writeFileSync("result.jpg", body);
});

Python

# Requires "requests" to be installed (see python-requests.org)
import requests

response = requests.post(
	'https://api.photoscissors.com/v1/change-background',
	files={'image': ('image.jpg', open('/path/to/file.jpg', 'rb'), 'image/jpeg')},
	data={'mode': 'image', 'format': 'jpg', 'background_color': '#FFFFFF'},
	headers={'X-Api-Key': 'INSERT_YOUR_API_KEY_HERE'},
)
if response.status_code == requests.codes.ok:
	with open('result.jpg', 'wb') as out:
		out.write(response.content)
else:
	print("Error:", response.status_code, response.text)

PHP

// Requires "guzzle" to be installed (see guzzlephp.org)

$client = new GuzzleHttp\Client();
$res = $client->post('https://api.photoscissors.com/v1/change-background', [
    'multipart' => [
        [
            'name'     => 'image',
            'contents' => fopen('/path/to/file.jpg', 'r')
        ],
        [
            'name'     => 'mode',
            'contents' => 'image'
        ],
		[
            'name'     => 'format',
            'contents' => 'jpg'
        ],
		[
            'name'     => 'background_color',
            'contents' => '#FFFFFF'
        ]
    ],
    'headers' => [
        'X-Api-Key' => 'INSERT_YOUR_API_KEY_HERE'
    ]
]);

$fp = fopen("result.jpg", "wb");
fwrite($fp, $res->getBody());
fclose($fp);

.NET

using (var client = new HttpClient())
using (var formData = new MultipartFormDataContent())
{
	var inFileStream = new FileStream("/path/to/file.jpg", FileMode.Open);
	StreamContent imageContent = new StreamContent(inFileStream);
	imageContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
	formData.Headers.Add("X-Api-Key", "INSERT_YOUR_API_KEY_HERE");
	formData.Add(imageContent, "image", "image.jpg");
	formData.Add(new StringContent("image"), "mode");
	formData.Add(new StringContent("jpg"), "format");
	formData.Add(new StringContent("#FFFFFF"), "backround_color");
	var response = client.PostAsync("https://api.photoscissors.com/v1/change-background", formData).Result;

	if (response.IsSuccessStatusCode)
	{
		FileStream fileStream = new FileStream("result.jpg", FileMode.Create, FileAccess.Write, FileShare.None);
		response.Content.CopyToAsync(fileStream).ContinueWith((copyTask) => { fileStream.Close(); });
	}
	else
	{
		Console.WriteLine("Error: " + response.Content.ReadAsStringAsync().Result);
	}
}

API Reference

POST: https://api.photoscissors.com/v1/change-background
Removes the background of a JPG/PNG image.
Output resolutions available: up to 4 megapixels

Parameters:

  • image
    Source image file (binary).
  • mode (image or alpha)
    image - returns image without background
    alpha - returs alpha mask.
  • format (jpg or png)
    Result image format
  • background_color
    hex color code
  • background_fit_to_result (true or false)
    Crop result image to the content rectangle