How to Build a Free, Offline AI Background Remover with Python

Dev.to / 3/25/2026

💬 OpinionDeveloper Stack & InfrastructureTools & Practical Usage

Key Points

  • The article explains how to build a completely offline background remover in Python that processes images in bulk without relying on paid web APIs.
  • It uses the `rembg` library (U2Net) as the AI engine for separating foreground from background, with a minimal code flow based on `new_session()` and `remove()`.
  • A lightweight Tkinter GUI is suggested to wrap the core logic into a user-friendly batch-processing application for entire folders of images.
  • PyInstaller is used to compile the Python project into a standalone executable (.exe) for easier distribution and use.
  • The author provides an open-source GitHub project and a YouTube tutorial for deeper implementation details, including GUI construction and how to compile the code.

Handling background removal usually means relying on paid APIs or web services that restrict your usage. Today, I'll share how I built a completely offline, bulk-processing background remover using Python.

The Core Stack
rembg (U2Net): The AI engine that accurately separates the foreground.

Tkinter: For a lightweight, built-in graphical interface.

PyInstaller: To compile the script into a standalone .exe.

The Magic Code
The actual background removal process is surprisingly straightforward when you use rembg sessions:

from rembg import remove, new_session
from PIL import Image

session = new_session()
input_img = Image.open('source.png')
result_img = remove(input_img, session=session)
result_img.save('transparent_result.png')

I expanded this simple logic into a full GUI application that supports batch processing for entire folders. It's incredibly useful for processing hundreds of images at once without internet access.

Dive Deeper
I've open-sourced the entire project. You can download the ready-to-use executable or fork the code from my GitHub repository:
💻 GitHub Repo: https://github.com/gohard-lab/batch_bg_remover

For a deep dive into the specific working principles, GUI construction, and how to compile the code yourself, please refer to my YouTube video guide:
📺 Watch the Tutorial: https://youtu.be/WR_D_LISU7A