Skip to content

This repository contains code samples to easily interact with the PhotoRoom API

License

Notifications You must be signed in to change notification settings

Photoroom/api-code-samples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

23 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

App Icon

join-slack api-calls license

PhotoRoom API Code Samples πŸ“Έ

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/

Table of Content

Web

Option A - Test the API with a demo webpage

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)

Option B - Integrate the API in your project

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);
        }
    }
});

Node

Option A - Test the API in the Terminal

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.

Option B - Integrate the API in your project

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);
    });

Python

Option A - Test the API in the Terminal

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.

Option B - Integrate the API in your project

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)

iOS

Option A - Test the API with an Xcode Playground

You can download a Playground that will enable you to easily call our API with a sample image:

remove_background_playground

To make it work, just get your apiKey and add it to the Playground:

remove_background_api_key

Option B - Integrate the API in your app

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 🍿

About

This repository contains code samples to easily interact with the PhotoRoom API

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published