Batch Image Segmentation with Python and PaddlePaddle
This tutorial demonstrates how to use Python and the PaddlePaddle deep‑learning platform to automatically remove backgrounds from multiple photos in one step, covering installation, verification, and a concise five‑line code example for batch human segmentation.
Ever wanted to extract a person from a photo and paste them elsewhere, even if you are far away? While Photoshop’s Magic Wand works for professionals and mobile apps help casual users, they handle only one image at a time and can be slow for complex pictures.
This article introduces a third approach: using Python to perform one‑click batch background removal.
Preparation
The backbone of this solution is PaddlePaddle (also known as “飞桨”), an open‑source deep‑learning platform that abstracts the low‑level framework so you can focus on creative tasks. Install the CPU version via pip:
python -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple
After installation, verify it in a Python session:
import paddle.fluid paddle.fluid.install_check.run_check()
If the output contains “Your Paddle works well on SINGLE GPU or CPU”, the installation succeeded.
Next, install paddlehub to access pre‑trained models:
pip install -i https://mirror.baidu.com/pypi/simple paddlehub
Code Implementation
The workflow is simple: import the module, load the model, collect image files, and call the segmentation API.
import os, paddlehub as hub huseg = hub.Module(name='deeplabv3p_xception65_humanseg') # load model path = './imgs/' # image directory files = [path + i for i in os.listdir(path)] # list of image files results = huseg.segmentation(data={'image': files}) # perform segmentation
Place your source images in an imgs folder next to the script. After running, the segmented images are saved in a humanseg_output directory with the same filenames but in PNG format.
Running the program on five sample pictures produces five output files where the detected persons are isolated on a white background, demonstrating that the method works for both single and multiple subjects.
Although some fine details may still have minor artifacts, the results are generally satisfactory and provide a handy tool for anyone who frequently needs to cut out people from photos.
In summary, using PaddlePaddle and a few lines of Python code enables fast, batch‑style background removal, freeing users from manual editing and adding a useful trick to any developer’s toolbox.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
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.