npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

rmbg

v0.1.0

Published

Remove image backgrounds in Node.js using AI models

Downloads

235

Readme

rmbg - Background Removal for Node.js

Remove image backgrounds in Node.js using AI models. Fast, privacy-focused (runs locally), and easy to use.

Features

  • 🔒 Privacy-focused: All processing happens locally, no server uploads
  • Fast: Optimized with ONNX Runtime for Node.js
  • 🎯 Simple API: Just await rmbg('image.jpg') - that's it!
  • 🔧 Flexible: Supports multiple input/output formats
  • 🎨 Multiple models: Choose between quality, speed, or file size
  • 📦 TypeScript: Full TypeScript support with type definitions
  • 🚀 Framework-ready: Works with Express, Next.js, serverless, etc.

Installation

npm install rmbg
# or
bun add rmbg

Quick Start

import { rmbg } from 'rmbg'

// Remove background from image
const output = await rmbg('input.jpg')

// Save to file
import { writeFileSync } from 'fs'
writeFileSync('output.png', output)

That's it! The background is removed and you get a PNG with transparency.

Usage

Basic Usage

import { rmbg } from 'rmbg'

// From file path
const output = await rmbg('./input.jpg')

// From URL
const output = await rmbg('https://example.com/image.jpg')

// From Buffer
import { readFileSync } from 'fs'
const buffer = readFileSync('./input.jpg')
const output = await rmbg(buffer)

// Save directly to file
await rmbg('input.jpg', { output: 'output.png' })

Using Different Models

import { rmbg } from 'rmbg'
import { createBriaaiModel, createModnetModel, createU2netpModel } from 'rmbg/models'

// High quality (1024px, 44MB) - best quality
const output = await rmbg('input.jpg', {
  model: createBriaaiModel()
})

// Medium quality (512px, 25MB) - balanced
const output = await rmbg('input.jpg', {
  model: createModnetModel()
})

// Fast (320px, 4.5MB) - fastest, default
const output = await rmbg('input.jpg', {
  model: createU2netpModel()
})

Progress Tracking

await rmbg('input.jpg', {
  onProgress: (progress, download, process) => {
    console.log(`Progress: ${Math.round(progress * 100)}%`)
    console.log(`Download: ${Math.round(download * 100)}%`)
    console.log(`Process: ${Math.round(process * 100)}%`)
  }
})

Advanced Options

await rmbg('input.jpg', {
  // Custom model
  model: createBriaaiModel(),

  // Maximum output resolution
  maxResolution: 4096,

  // Save to file
  output: 'output.png',

  // Progress callback
  onProgress: (progress) => console.log(`${progress * 100}%`),

  // Abort controller for cancellation
  abortController: new AbortController(),

  // Cache directory (default: os.tmpdir())
  cacheDir: '/tmp/rmbg-models',

  // Enable model caching (default: true)
  enableCache: true
})

Framework Integration

Express.js

import express from 'express'
import multer from 'multer'
import { rmbg } from 'rmbg'

const app = express()
const upload = multer()

app.post('/remove-bg', upload.single('image'), async (req, res) => {
  try {
    const output = await rmbg(req.file.buffer)
    res.contentType('image/png').send(output)
  } catch (error) {
    res.status(500).json({ error: error.message })
  }
})

app.listen(3000)

Next.js API Route

// pages/api/remove-background.ts
import { rmbg } from 'rmbg'
import { NextApiRequest, NextApiResponse } from 'next'

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  if (req.method !== 'POST') {
    return res.status(405).json({ error: 'Method not allowed' })
  }

  try {
    // Get image from request body (multipart form data)
    const formData = await parseForm(req) // Use multiparty, formidable, etc.
    const output = await rmbg(formData.image)

    res.setHeader('Content-Type', 'image/png')
    res.send(output)
  } catch (error) {
    res.status(500).json({ error: error.message })
  }
}

AWS Lambda

import { rmbg } from 'rmbg'
import { APIGatewayProxyHandler } from 'aws-lambda'

export const handler: APIGatewayProxyHandler = async (event) => {
  try {
    const imageBuffer = Buffer.from(event.body, 'base64')
    const output = await rmbg(imageBuffer)

    return {
      statusCode: 200,
      headers: { 'Content-Type': 'image/png' },
      body: output.toString('base64'),
      isBase64Encoded: true
    }
  } catch (error) {
    return {
      statusCode: 500,
      body: JSON.stringify({ error: error.message })
    }
  }
}

API Reference

rmbg(input, options?)

Remove background from an image.

Parameters:

  • input: string | Buffer | Readable - Image input (file path, URL, Buffer, or stream)
  • options: RMBGOptions (optional) - Configuration options

Returns: Promise<Buffer> - Processed image as PNG buffer

Options:

  • model: RMBGModel - AI model to use (default: u2netp)
  • maxResolution: number - Maximum output resolution (default: 2048)
  • output: string | Writable - Output destination (file path or stream)
  • onProgress: (progress, download, process) => void - Progress callback
  • abortController: AbortController - For cancellation support
  • cacheDir: string - Model cache directory (default: os.tmpdir())
  • enableCache: boolean - Enable model caching (default: true)

Model Functions

All model functions accept an optional publicPath parameter to customize the CDN URL.

  • createU2netpModel(publicPath?) - Fast (320px, 4.5MB) - Default
  • createModnetModel(publicPath?) - Medium (512px, 25MB)
  • createBriaaiModel(publicPath?) - High quality (1024px, 44MB)

Performance

Performance depends on the model and image size:

  • u2netp (default): 1-2 seconds for typical images
  • modnet: 2-4 seconds for typical images
  • briaai: 4-8 seconds for typical images

Models are cached after first download for faster subsequent use.

Environment Variables

  • RMBG_CACHE_DIR: Custom cache directory for models
  • RMBG_ENABLE_CACHE: Set to false to disable caching

System Requirements

  • Node.js 18+ (LTS versions)
  • Supported platforms: Linux, macOS, Windows
  • Native dependencies: Sharp, ONNX Runtime Node

License

MIT

Related Packages

Credits

Powered by ONNX Runtime and Sharp for high-performance image processing.