Batch Background Removal with PaddlePaddle: One-Click Python Script

This guide shows how to install PaddlePaddle and PaddleHub, then use a short Python script to automatically segment and remove backgrounds from multiple photos, turning each into a transparent PNG with just a few lines of code.

ITPUB
ITPUB
ITPUB
Batch Background Removal with PaddlePaddle: One-Click Python Script

Preparation

We first install the PaddlePaddle deep-learning framework (CPU version) using the official mirror:

python -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple

After installation we verify it in a Python session:

import paddle.fluid
paddle.fluid.install_check.run_check()

The output ends with “Your Paddle works well … Your Paddle is installed successfully!” indicating a successful setup.

Next we install PaddleHub, which provides ready-made models:

pip install -i https://mirror.baidu.com/pypi/simple paddlehub

Code Implementation

The workflow consists of four steps: import the module, load the segmentation model, collect image files, and run the segmentation.

import os, paddlehub as hub
huseg = hub.Module(name='deeplabv3p_xception65_humanseg')  # load model
path = './imgs/'                                          # image folder
files = [path + i for i in os.listdir(path)]               # list files
results = huseg.segmentation(data={'image': files})       # perform cutout

Place the source pictures in the imgs directory. After running the script, PaddleHub saves the results in a humanseg_output folder, preserving the original filenames but with a .png extension and a white background where the subject was removed.

Result

Below are examples of the original photos and the corresponding cut-out images produced by the script.

Original images
Original images
Background-removed images
Background-removed images

The tool correctly isolates people (single or multiple) and outputs them with a white background; minor artifacts may appear on fine details, but the overall quality is satisfactory.

Conclusion

Using PaddlePaddle and PaddleHub, a five-line Python program can batch-process images to remove backgrounds, saving time and effort for developers who need quick visual effects.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Batch Processingimage segmentationPaddlePaddle
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.