How to remove background

In this guide, learn how to remove backgrounds from images using the Picsart TypeScript SDK. By creating a RemoveBackgroundRequest object and setting the image source, you can easily manipulate images and replace backgrounds. Detailed instructions and code examples help you navigate the process and effectively manage API results, providing valuable insights into API usage and limits. Start enhancing your image processing capabilities with this comprehensive tutorial.

Before you start

Check these out before you start

Remove Background from an Image

In Getting Started with TypeScript SDK you have already learned how to create your image source. Now, you can remove the background from that image using the remove background service.

You need to create a RemoveBackgroundRequest object, set the image source, and optionally specify a background color:

const result = await imageApi.removeBackground(  
    new RemoveBackgroundRequest()  
      .setImage(imageSource) // you can use binary, stream, URL to create this image source
      .setBgColor("green")  
);

In this example, the background of the image will be removed and replaced with a green background.

To discover all available parameters for the remove background service,

Handling the Result

After the background removal process is complete, you can access various pieces of information from the result:

console.log(result.image.url); // The URL of the processed image  
console.log(result.metadata.creditsAvailable); // Credits available for the account  
console.log(result.metadata.rateLimitResetTime); // Time until the rate limit resets  
console.log(result.metadata.rateLimit); // Current rate limit for API usage  
console.log(result.metadata.rateLimitAvailable); // Remaining rate limit  
console.log(result.metadata.correlationId); // Correlation ID for tracking requests

These metadata fields provide insights into the usage of the API and can help you manage your application's API calls more effectively. These fields are available throughout all the API methods.

What's next:

See other operations you can perform on an image:

For more examples, check out the TypeScript SDK Repository.