This repository contains code samples to easily interact with our API.
More details about PhotoRoom's API 👉 https://www.photoroom.com/api
Link to the full documentation 👉 https://docs.photoroom.com/docs/api/
You can clone this repository and locally serve this demo webpage.
To make it work, just get your apiKey
and add it to the file remove-background.ts.
(you will need to run tsc
in order to update the JavaScript code)
To integrate our API inside your website, just copy/paste the content of the file remove-background.ts into your project.
Then, here's an example of how you can call the API:
import { removeBackground } from './remove-background.js';
const fileInput = document.getElementById('fileInput') as HTMLInputElement;
const displayImage = document.getElementById('displayImage') as HTMLImageElement;
fileInput.addEventListener('change', async () => {
const files = fileInput.files;
if (files && files.length > 0) {
try {
const imageBlob = await removeBackground(files[0]); // 👈 API call is here
const objectURL = URL.createObjectURL(imageBlob);
displayImage.src = objectURL;
} catch (error) {
console.error('Error:', error);
}
}
});
You can clone this repository and run these two commands in the Terminal:
$ cd api-code-samples/Node
$ node demo.js
To make it work, just get your apiKey
and add it to the file remove-background.js.
To integrate our API inside your project, just copy/paste the content of the file remove-background.js into your Node project.
Then, here's an example of how you can call the API:
const removeBackground = require('./remove-background');
const imagePath = './path/to/your/image.jpg';
const savePath = './path/where/you/want/to/save/response.jpg';
removeBackground(imagePath, savePath)
.then(message => {
console.log(message);
})
.catch(error => {
console.error('Error:', error);
});
You can clone this repository and run these two commands in the Terminal:
$ cd api-code-samples/Python
$ python demo.py
To make it work, just get your apiKey
and add it to the file remove_background.py.
To integrate our API inside your project, just copy/paste the content of the file remove_background.py into your Python project.
Then, here's an example of how you can call the API:
from remove_background import remove_background
# Example usage
input_path = "test.jpg"
output_path = "result.png"
remove_background(input_path, output_path)
You can download a Playground that will enable you to easily call our API with a sample image:
To make it work, just get your apiKey
and add it to the Playground:
To integrate our API inside your app, just copy/paste the content of the file RemoveBackground.swift into your Xcode project.
Then, depending on whether you are using Swift Concurrency, you can call either:
// with Swift Concurrency
Task { @MainActor in
imageView.image = try await removeBackground(of: yourImage)
}
or
// without Swift Concurrency
removeBackground(of: yourImage) { result in
switch result {
case let .success(backgroundRemoved):
imageView.image = backgroundRemoved
case let .failure(error):
// handle the `error`
}
}
You can also watch this short demo 🍿