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.
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 cutoutPlace 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.
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
