Giter VIP home page Giter VIP logo

api-code-samples's Introduction

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 ๐Ÿฟ

api-code-samples's People

Contributors

eliotandres avatar vincent-pradeilles avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

api-code-samples's Issues

BrokenPipeError, how to deal with it?

File "/usr/local/Cellar/[email protected]/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1051, in _send_output
self.send(chunk)
File "/usr/local/Cellar/[email protected]/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 973, in send
self.sock.sendall(data)
File "/usr/local/Cellar/[email protected]/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1204, in sendall
v = self.send(byte_view[count:])
File "/usr/local/Cellar/[email protected]/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1173, in send
return self._sslobj.write(data)
BrokenPipeError: [Errno 32] Broken pipe

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.