Use Cases & Solutions

Remove Product Photo Backgrounds in Bulk with AI (rembg): Transparent PNGs and White-Background Main Images

5 steps 13 min read 22 views 0
On this page

Marketplaces such as Amazon require main product images on a pure white background, and listings on Shopee, Lazada and TikTok Shop look more professional with consistent square images. Cutting out hundreds of photos by hand takes days. rembg is an open-source tool that runs AI background-removal models on an ordinary CPU server. This guide installs it, compares the models, and uses a short script to turn a folder of product photos into transparent PNG files and square white-background JPG main images in one run.

Use only photos you own or have the rights to use. Check the results before uploading: thin cables, transparent glass, fur and hair are the hardest cases for any background-removal model.

Step 1: Install rembg

apt install -y python3-venv
python3 -m venv /opt/rembg
/opt/rembg/bin/pip install "rembg[cpu,cli]"
/opt/rembg/bin/rembg --help

Each model is downloaded automatically the first time it is used and stored in ~/.u2net, so the server needs internet access on the first run.

Step 2: Try a Single Photo and Choose a Model

/opt/rembg/bin/rembg i photo.jpg cut-u2net.png                          # default model
/opt/rembg/bin/rembg i -m isnet-general-use photo.jpg cut-isnet.png     # often cleaner edges
/opt/rembg/bin/rembg i -m birefnet-general photo.jpg cut-birefnet.png   # most accurate, slower, more RAM
/opt/rembg/bin/rembg i -a photo.jpg cut-matting.png                     # alpha matting for hair and soft edges
  • u2net: the general-purpose default.
  • isnet-general-use: usually sharper product edges at similar speed; a good default for e-commerce.
  • birefnet-general: the most accurate on difficult photos, but several times slower and needs more memory.
  • u2netp: a small, fast model for quick previews.

Run the candidates on five typical photos from your catalogue and compare the edges at full size before choosing.

Step 3: Create the Batch Script

cat > /opt/rembg/whitebg.py <<'EOF2'
import os, sys
from PIL import Image, ImageOps
from rembg import new_session, remove

src, dst = sys.argv[1], sys.argv[2]
size = int(os.environ.get("SIZE", "1600"))      # square canvas in pixels
fill = float(os.environ.get("FILL", "0.85"))    # share of the canvas the product fills
session = new_session(os.environ.get("MODEL", "isnet-general-use"))
for sub in ("png", "white"):
    os.makedirs(os.path.join(dst, sub), exist_ok=True)

for name in sorted(os.listdir(src)):
    if not name.lower().endswith((".jpg", ".jpeg", ".png", ".webp")):
        continue
    base = os.path.splitext(name)[0]
    img = ImageOps.exif_transpose(Image.open(os.path.join(src, name))).convert("RGB")
    cut = remove(img, session=session)
    box = cut.getchannel("A").getbbox()
    if not box:
        print(name, "no object found, skipped", flush=True)
        continue
    cut = cut.crop(box)                                   # trim the empty space around the product
    cut.save(os.path.join(dst, "png", base + ".png"))     # transparent version
    scale = size * fill / max(cut.size)
    cut = cut.resize((max(1, round(cut.width * scale)), max(1, round(cut.height * scale))),
                     Image.Resampling.LANCZOS)
    canvas = Image.new("RGB", (size, size), (255, 255, 255))
    canvas.paste(cut, ((size - cut.width) // 2, (size - cut.height) // 2), cut)
    canvas.save(os.path.join(dst, "white", base + ".jpg"), quality=92)
    print(name, "done", flush=True)
EOF2

The script corrects the rotation of phone photos, removes the background, trims the empty edges, saves a transparent PNG, then centres the product on a pure white (255, 255, 255) square canvas so that it fills about 85% of the frame.

Step 4: Process a Folder

mkdir -p /data/photos/in /data/photos/out     # upload the original photos to /data/photos/in
cd /data/photos
nohup /opt/rembg/bin/python /opt/rembg/whitebg.py in out > whitebg.log 2>&1 &
tail -f whitebg.log

# Other sizes or models, for example:
SIZE=2000 FILL=0.8 MODEL=birefnet-general /opt/rembg/bin/python /opt/rembg/whitebg.py in out

The results are in two folders: out/png holds transparent cut-outs for banners, ads and design work, and out/white holds the square main images ready for upload. Speed depends on the model and the CPU; time a batch of 20 photos first to estimate how long the whole catalogue will take.

Step 5: Check and Fix the Difficult Ones

  • View the PNG files on a dark background: leftover halos and missing parts are much easier to see there than on white.
  • For hair, fur and soft edges, re-run those photos with alpha matting: add alpha_matting=True to the remove() call, or use rembg i -a for single files. It is slower, so use it only where needed.
  • Models remove shadows. If you want a natural shadow under the product, add it in your design tool from the transparent PNG.
  • For white products on a white canvas, consider a light grey background for secondary images; check each marketplace's rules for main images.

FAQ

Do I need a GPU?

No. For tens or hundreds of photos a day, a CPU server is enough. A GPU only makes a real difference when you process many thousands of images daily or use the largest models all the time.

Can colleagues use it without SSH?

rembg includes a small web server (rembg s) with a simple page and an HTTP API. Run it on 127.0.0.1 and publish it through Nginx with HTTPS and a password, never directly on a public port.

What else can AI automate for product listings?

Titles, bullet points and descriptions in several languages, see writing multilingual product listings in bulk with AI, and subtitles for product videos, see automatic subtitles with faster-whisper.

Still stuck after following these steps? Open a support ticket and the IMIDC 24/7 technical team will help. Include the server IP, CPU and RAM, the model you used and the last lines of whitebg.log so we can pinpoint the issue faster.

Was this answer helpful?

Related Tutorials