Batch Remove Image Backgrounds with Python and PaddlePaddle
This tutorial shows how to install PaddlePaddle and PaddleHub, then use a concise Python script to batch‑remove backgrounds from multiple photos, automatically saving the segmented results, and includes sample code and before‑after images to illustrate the process.
Preparation
We first install the PaddlePaddle deep‑learning framework (CPU version) via pip and verify the installation, then install PaddleHub.
python -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple import paddle.fluid
paddle.fluid.install_check.run_check() pip install -i https://mirror.baidu.com/pypi/simple paddlehubCode Implementation
The workflow consists of importing the module, loading the model, collecting image files, and calling the segmentation API.
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)] # file list
results = huseg.segmentation(data={'image': files}) # perform cutoutPlace the source pictures in the imgs directory; after running the script the cut‑out images are saved in humanseg_output with the same filenames but PNG format.
Example original images:
Resulting cut‑out images:
Conclusion
This short script demonstrates how PaddlePaddle and PaddleHub can batch‑process image background removal with only a few lines of Python code, saving time for developers who need quick visual editing.
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.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
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.
