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.
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.
/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
Run the candidates on five typical photos from your catalogue and compare the edges at full size before choosing.
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.
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.
alpha_matting=True to the remove() call, or use rembg i -a for single files. It is slower, so use it only where needed.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.
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.
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.