UNPKG

@remove-background-ai/rembg.js

Version:

A simple wrapper for the https://www.rembg.com API

55 lines (54 loc) 2.65 kB
/// <reference types="node" /> import { AxiosProgressEvent } from 'axios'; type InputType = string | Buffer | { base64: string; }; type Options = { returnMask?: boolean; returnBase64?: boolean; w: number; h: number; exact_resize?: boolean; format?: "png" | "PNG" | "webp" | "WEBP"; angle?: number; expand?: boolean; bg_color?: string; }; /** * Removes the background from an image using the rembg.com API. * * @param apiKey - The API key for rembg.com. * @param inputImage - file path, Buffer, or an object with a { base64: string } property. * @param onUploadProgress - A callback function to handle upload progress events. Defaults to console.log. * @param onDownloadProgress - A callback function to handle download progress events. Defaults to console.log. * @param options - set of options for image Post processing. * @param options.returnMask - If true, returns the alpha-Matte (mask) image instead of the image. * @param options.returnBase64 - If true, returns the output image as a Base64 string instead of saving it to a file. * @param options.w - The width of the output image. * @param options.h - The height of the output image. * @param options.exact_resize - If true, the output image will be resized to the specified width and height. * @param options.format - format the image to target format, by default it is WEBP. * @param options.angle - Rotation angle in degrees (optional). * @param options.expand - Add padding so rotated images aren't cropped (optional, defaults to true). * @param options.bg_color - Optional solid background color in hex (e.g. #FFFFFFFF) or named color (e.g. "red", "blue"). * @returns If returnBase64 is true, returns an object with the base64Image property containing the Base64 string of the output image. * If returnBase64 is false, returns an object with the outputImagePath property containing the path to the output image file, * and the cleanup function to delete the temporary file. * @throws Throws an error if the API key is not provided or if the request fails. */ export declare const rembg: ({ apiKey, inputImage, options, onUploadProgress, onDownloadProgress, }: { apiKey: string; inputImage: InputType; options?: Options | undefined; onUploadProgress: (progressEvent: AxiosProgressEvent) => void; onDownloadProgress: (progressEvent: AxiosProgressEvent) => void; }) => Promise<{ base64Image: string; outputImagePath?: undefined; cleanup?: undefined; } | { outputImagePath: string; cleanup: () => Promise<void>; base64Image?: undefined; }>; export default rembg;