Remove Image Background Programmatically via PhotoScissors API
Looking to programmatically remove image backgrounds? In addition to the online version, PhotoScissors provides an API that allows you to seamlessly integrate background removal capabilities into your app or website with just a few lines of code.
Integrating background removal into your application or website is a straightforward process. Follow these steps to get started quickly:
- Step 1: Obtain your API Key.
- Step 2: Utilize the provided code samples
We provide code samples to assist you in implementing the background removal functionality. Use these samples as a starting point for your development process. With just a few lines of code, you can leverage the power of the PhotoScissors API.
cURL
curl -H 'X-API-Key: INSERT_YOUR_API_KEY_HERE' \ -F 'image=@/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
const axios = require('axios'); const FormData = require('form-data'); const fs = require('fs'); const formData = new FormData(); formData.append('mode', 'image'); formData.append('format', 'jpg'); formData.append('backround_color', '#FFFFFF'); formData.append('image', fs.createReadStream('/path/to/file.jpg')); axios({ method: 'post', url: 'https://api.photoscissors.com/v1/change-background', data: formData, responseType: 'arraybuffer', headers: { ...formData.getHeaders(), 'X-Api-Key': 'INSERT_YOUR_API_KEY_HERE', } }).then((response) => { if (response.status != 200) return console.error('Error:', response.status, response.statusText); fs.writeFileSync("result.jpg", response.data); }).catch((error) => { return console.error('Request failed:', error); });
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