Dev48
Language
  • About
  • Services
  • Industries
  • Technologies
  • Articles
  • Contacts
Book a call
    Home/Articles/How to remove image backgrounds in python 2027 guide
Dev48
info@dev48.org+375 29 320-40-96WhatsApp@isky_by

© 2026 · All rights reserved.

How to remove image backgrounds in Python (2027 guide)

Source: Bria AI•December 2, 2026 at 09:29 AM

How to remove image backgrounds in Python (2027 guide)

Updated 2026-12-01 TL;DR Direct answer: install rembg, open the file with Pillow, call remove() and save a PNG. For a hosted route, call the Bria API from the bria-client SDK. Install: pip install "rembg[cpu]" pillow. Use rembg[gpu] if you have a supported GPU. Edges: rembg has 4 edge modes.…

Updated 2026-12-01

TL;DR

  • Direct answer: install rembg, open the file with Pillow, call remove() and save a PNG. For a hosted route, call the Bria API from the bria-client SDK.

Direct answer: install rembg, open the file with Pillow, call remove() and save a PNG. For a hosted route, call the Bria API from the bria-client SDK.

  • Install: pip install "rembg[cpu]" pillow. Use rembg[gpu] if you have a supported GPU.

Install: pip install "rembg[cpu]" pillow. Use rembg[gpu] if you have a supported GPU.

  • Edges: rembg has 4 edge modes. Start with decontaminate=True and reach for alpha matting only on hair or fur.

Edges: rembg has 4 edge modes. Start with decontaminate=True and reach for alpha matting only on hair or fur.

  • Batch: create one session with new_session() and reuse it for every file, or run rembg p on a folder.

Batch: create one session with new_session() and reuse it for every file, or run rembg p on a folder.

  • API: the Bria API costs $0.018 per image for RMBG 3.0 (checked 2026-11-29) and runs as an async job you poll.

API: the Bria API costs $0.018 per image for RMBG 3.0 (checked 2026-11-29) and runs as an async job you poll.

  • Pick by workload: local rembg is enough for scripts and prototypes. An API pays off when you need no GPU, a commercial path and predictable throughput.

Pick by workload: local rembg is enough for scripts and prototypes. An API pays off when you need no GPU, a commercial path and predictable throughput.

Bria wrote this guide. Any benchmark figure in it is Bria's own benchmark, built from blind votes by professional and casual users plus a VLM side by side. The code below is copied from the rembg and Bria documentation and was not run for this article, so test it on your own images first.

How do I install rembg and cut out one image?

Install `rembg[cpu]` with Pillow, open the image, call `remove()` and save the result as PNG. The rembg README lists Python 3.11 up to, but not including, 3.14, and offers rembg[cpu], rembg[gpu] and rembg[rocm] installs (checked 2026-12-01).

  • The first call downloads the model to ~/.rembg/models/, so it runs slower once.

The first call downloads the model to ~/.rembg/models/, so it runs slower once.

  • The default model is bria-rmbg, which is RMBG 2.0, about 1.02 GB.

The default model is bria-rmbg, which is , about 1.02 GB.

  • remove() takes a PIL image, bytes or a NumPy array, and returns the same kind of object.

remove() takes a PIL image, bytes or a NumPy array, and returns the same kind of object.

  • Save as PNG. JPEG has no alpha channel, so Pillow raises an error when you save an RGBA cutout as JPEG.

Save as PNG. JPEG has no alpha channel, so Pillow raises an error when you save an RGBA cutout as JPEG.

How do I choose a model in rembg?

Pass a model name to `new_session()` and hand the session to `remove()`. The README lists models such as u2net, u2netp, silueta, isnet-general-use and the birefnet-* family, plus bria-rmbg as the default.

  • BiRefNet powers the birefnet-* models. Its Hugging Face card lists an MIT license.

powers the birefnet-* models. Its Hugging Face card lists an MIT license.

  • U2-Net powers u2net. The README says -m u2net gives a smaller, faster model with blockier edges.

powers u2net. The README says -m u2net gives a smaller, faster model with blockier edges.

  • For portraits, the README suggests birefnet-portrait with -dc as a good start.

For portraits, the README suggests birefnet-portrait with -dc as a good start.

  • Model weights carry their own licenses, independent of rembg's MIT license. Read the license of the model you ship.

Model weights carry their own licenses, independent of rembg's MIT license. Read the license of the model you ship.

What do the rembg edge options do?

Start with `decontaminate=True`, then try alpha matting only if soft edges still look wrong. The README's edge-mode table covers 4 modes, and the keyword arguments below come from rembg's remove() signature and USAGE.md.

  • Edge mode: Naive (default); How to set it in Python: remove(img); Notes from the README: Fast, no extra step

Edge mode: Naive (default); How to set it in Python: remove(img); Notes from the README: Fast, no extra step

  • Edge mode: Decontaminate; How to set it in Python: remove(img, decontaminate=True); Notes from the README: Removes color fringing, alpha unchanged

Edge mode: Decontaminate; How to set it in Python: remove(img, decontaminate=True); Notes from the README: Removes color fringing, alpha unchanged

  • Edge mode: Alpha matting; How to set it in Python: remove(img, alpha_matting=True); Notes from the README: Slow, helps most with older models on hair

Edge mode: Alpha matting; How to set it in Python: remove(img, alpha_matting=True); Notes from the README: Slow, helps most with older models on hair

  • Edge mode: ViTMatte; How to set it in Python: remove(img, vitmatte=True); Notes from the README: Slow, extra download of about 110 MB

Edge mode: ViTMatte; How to set it in Python: remove(img, vitmatte=True); Notes from the README: Slow, extra download of about 110 MB

  • Newer models such as bria-rmbg and the birefnet-* family rarely need alpha matting, per the README.

Newer models such as bria-rmbg and the birefnet-* family rarely need alpha matting, per the README.

  • Older models such as u2net and silueta give blockier edges and gain the most from it.

Older models such as u2net and silueta give blockier edges and gain the most from it.

  • For batches the README prefers decontamination over alpha matting.

For batches the README prefers decontamination over alpha matting.

  • post_process_mask=True post-processes the mask, and force_return_bytes=True always returns bytes.

post_process_mask=True post-processes the mask, and force_return_bytes=True always returns bytes.

How do I remove backgrounds from a whole folder?

Create one session and reuse it, so the model loads once instead of once per image. The README shows this pattern for folders of files:

  • The same job runs from a shell with rembg p, which processes a folder. Install the CLI extra with pip install "rembg[cpu,cli]".

The same job runs from a shell with rembg p, which processes a folder. Install the CLI extra with pip install "rembg[cpu,cli]".

  • Passing bytes in returns bytes out, so you can write the result straight to disk or storage.

Passing bytes in returns bytes out, so you can write the result straight to disk or storage.

How do I call the Bria API from Python?

Install `bria-client`, set `BRIA_API_TOKEN`, then submit a job to `image/edit/remove_background` and poll it. The SDK needs Python 3.10 or newer and is MIT licensed (Python SDK docs, checked 2026-12-01).

  • Image() accepts a URL, a local path, a PIL image or a NumPy array.

Image() accepts a URL, a local path, a PIL image or a NumPy array.

  • model takes RMBG2 (the default) or RMBG3. Sending prompt with RMBG 3.0 returns a 422.

model takes RMBG2 (the default) or RMBG3. Sending prompt with RMBG 3.0 returns a 422.

  • preserve_alpha and webhook_url are optional request fields in the background removal reference.

preserve_alpha and webhook_url are optional request fields in the background removal reference.

  • Input is JPEG, PNG or WEBP, sent as a URL or base64. Other types return 415.

Input is JPEG, PNG or WEBP, sent as a URL or base64. Other types return 415.

  • Do not put sync in the payload. client.run(...) sets it for you.

Do not put sync in the payload. client.run(...) sets it for you.

  • The API answers 200 with result.image_url for a synchronous call, or 202 with a request_id and status_url for an async job.

The API answers 200 with result.image_url for a synchronous call, or 202 with a request_id and status_url for an async job.

The Bria API request fields shown here are the ones the reference documents. This guide does not show edge-tuning options for the API.

How do I batch images through the Bria API?

Loop over files, submit and poll each one, download the result and pace requests under your plan's limit. Limits are 10 requests per minute on Free, 60 on Pay-as-you-go and 1000 on Enterprise, and going over returns 429 (rate limits and errors).

On the Free tier (10 requests per minute), raise the time.sleep and the poll interval well above 2 seconds.

  • Status polls count toward the limit, so each image costs at least two requests. At 60 requests per minute, 600 images take about 20 minutes.

Status polls count toward the limit, so each image costs at least two requests. At 60 requests per minute, 600 images take about 20 minutes.

  • Status calls count toward the limit, so poll with backoff and honor Retry-After on a 429.

Status calls count toward the limit, so poll with backoff and honor Retry-After on a 429.

  • For big queues, use webhook_url so you do not poll at all.

For big queues, use webhook_url so you do not poll at all.

When is local rembg enough, and when does an API pay off?

Use local rembg for scripts, prototypes and one-off folders. Use the API when the code ships to paying users or you have no GPU. The table sets out the trade-offs.

  • Question: Install; Local rembg: pip install "rembg[cpu]"; Bria API: pip install bria-client plus an API key

Question: Install; Local rembg: pip install "rembg[cpu]"; Bria API: pip install bria-client plus an API key

  • Question: Hardware; Local rembg: Your CPU or GPU; Bria API: None on your side

Question: Hardware; Local rembg: Your CPU or GPU; Bria API: None on your side

  • Question: Cost per image; Local rembg: No fee, you pay for hardware; Bria API: $0.018 for RMBG 3.0 (checked 2026-11-29)

Question: Cost per image; Local rembg: No fee, you pay for hardware; Bria API: $0.018 for RMBG 3.0 (checked 2026-11-29)

  • Question: Commercial use; Local rembg: Default model needs a paid agreement; Bria API: Commercial path is the API

Question: Commercial use; Local rembg: Default model needs a paid agreement; Bria API: Commercial path is the API

  • Question: Throughput; Local rembg: Bound by your machine; Bria API: Bound by the plan limit

Question: Throughput; Local rembg: Bound by your machine; Bria API: Bound by the plan limit

  • Arithmetic at $0.018 per image: 10,000 images cost $180.

Arithmetic at $0.018 per image: 10,000 images cost $180.

  • The rembg README says the RMBG 2.0 license requires a paid agreement for commercial use.

The rembg README says the RMBG 2.0 license requires a paid agreement for commercial use.

  • Bria's benchmark placed RMBG 3.0 first among 6 models, with an average rank of 1.51 over 234 hard test images. The benchmark write-up explains the method.

Bria's benchmark placed RMBG 3.0 first among 6 models, with an average rank of 1.51 over 234 hard test images. The benchmark write-up explains the method.

  • The RMBG 3.0 weights are CC BY-NC 4.0, with commercial use needing a separate agreement. See bria.ai/rmbg for the commercial options.

The weights are CC BY-NC 4.0, with commercial use needing a separate agreement. See bria.ai/rmbg for the commercial options.

What should I check before shipping?

Test on your own hardest images, confirm the model license and set a failure path for 429 and 415 responses.

  • Run 20 to 50 of your worst photos through each route and compare edges by eye.

Run 20 to 50 of your worst photos through each route and compare edges by eye.

  • Record which model each output came from.

Record which model each output came from.

  • Log request IDs so a failed job can be retried.

Log request IDs so a failed job can be retried.

  • Re-check prices and limits on the linked pages, because both change.

Re-check prices and limits on the linked pages, because both change.

Which route should you choose?

  • Choose local rembg if you run scripts, prototypes or one-off folders on hardware you control and want no per-image fee.

Choose local rembg if you run scripts, prototypes or one-off folders on hardware you control and want no per-image fee.

  • Choose the Bria API if the code ships to paying users or you have no GPU. It is hosted, at $0.018 per image for RMBG 3.0 (checked 2026-11-29).

Choose the Bria API if the code ships to paying users or you have no GPU. It is hosted, at $0.018 per image for RMBG 3.0 (checked 2026-11-29).

  • Check the CC BY-NC 4.0 license first if you plan to run the open RMBG 3.0 weights commercially. See bria.ai/rmbg for the commercial options.

Check the CC BY-NC 4.0 license first if you plan to run the open RMBG 3.0 weights commercially. See bria.ai/rmbg for the commercial options.

← All articles

What this article says

More in AI & Machine Learning

All →
Best background removal model in 2027: 6 models, 234 images
Bria AI

Best background removal model in 2027: 6 models, 234 images

Bria vs remove.bg: API, price and quality compared (2027)
Bria AI

Bria vs remove.bg: API, price and quality compared (2027)

6 best remove.bg alternatives in 2027 by volume cost
Bria AI

6 best remove.bg alternatives in 2027 by volume cost

You can now use Claude by talking to your Apple WatchПресса
Claude

You can now use Claude by talking to your Apple Watch

RAG vs CAG: We Measured Accuracy and Cost at 3 Sizes
OpenRouter

RAG vs CAG: We Measured Accuracy and Cost at 3 Sizes

How Much Does an AI Token Cost? Real Prices, Measured
OpenRouter

How Much Does an AI Token Cost? Real Prices, Measured

More from Bria AI

Best background removal model in 2027: 6 models, 234 images
Bria AI

Best background removal model in 2027: 6 models, 234 images

Bria vs remove.bg: API, price and quality compared (2027)
Bria AI

Bria vs remove.bg: API, price and quality compared (2027)

6 best remove.bg alternatives in 2027 by volume cost
Bria AI

6 best remove.bg alternatives in 2027 by volume cost

Best background remover for hard images in 2027
Bria AI

Best background remover for hard images in 2027

Why AI Product Images Get Rejected: The Production-Ready Bar
Bria AI

Why AI Product Images Get Rejected: The Production-Ready Bar

The Hidden Multiplier: What Localization Actually Costs
Bria AI

The Hidden Multiplier: What Localization Actually Costs