Removing the background of an image that has a clearly defined background is easy with Python. I found this useful tip that allows this to be performed using a very simple Python script.
Install the module.
(jcartwright@2403-4800-25af-b00--2) 192.168.1.5 Videos $ pip install rembg Defaulting to user installation because normal site-packages is not writeable Collecting rembg Downloading rembg-2.0.56-py3-none-any.whl (32 kB) Collecting pillow Downloading pillow-10.3.0-cp39-cp39-manylinux_2_28_x86_64.whl (4.5 MB) |████████████████████████████████| 4.5 MB 4.8 MB/s Collecting pooch Downloading pooch-1.8.1-py3-none-any.whl (62 kB) |████████████████████████████████| 62 kB 241 kB/s Collecting jsonschema Downloading jsonschema-4.21.1-py3-none-any.whl (85 kB) |████████████████████████████████| 85 kB 791 kB/s Collecting scikit-image Downloading scikit_image-0.22.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (14.7 MB) |████████████████████████████████| 14.7 MB 6.2 MB/s Collecting pymatting Downloading PyMatting-1.1.12-py3-none-any.whl (52 kB) |████████████████████████████████| 52 kB 198 kB/s Collecting tqdm Downloading tqdm-4.66.2-py3-none-any.whl (78 kB) |████████████████████████████████| 78 kB 1.1 MB/s Requirement already satisfied: numpy in /home/jcartwright/.local/lib/python3.9/site-packages (from rembg) (1.26.3) Collecting onnxruntime Downloading onnxruntime-1.17.1-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (6.8 MB) |████████████████████████████████| 6.8 MB 6.2 MB/s Collecting opencv-python-headless Downloading opencv_python_headless-4.9.0.80-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (49.6 MB) |████████████████████████████████| 49.6 MB 6.3 MB/s Collecting scipy Downloading scipy-1.13.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (38.6 MB) |████████████████████████████████| 38.6 MB 84 kB/s Collecting referencing>=0.28.4 Downloading referencing-0.34.0-py3-none-any.whl (26 kB) Collecting rpds-py>=0.7.1 Downloading rpds_py-0.18.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB) |████████████████████████████████| 1.1 MB 5.4 MB/s Collecting jsonschema-specifications>=2023.03.6 Downloading jsonschema_specifications-2023.12.1-py3-none-any.whl (18 kB) Collecting attrs>=22.2.0 Downloading attrs-23.2.0-py3-none-any.whl (60 kB) |████████████████████████████████| 60 kB 711 kB/s Collecting coloredlogs Downloading coloredlogs-15.0.1-py2.py3-none-any.whl (46 kB) |████████████████████████████████| 46 kB 604 kB/s Requirement already satisfied: packaging in /home/jcartwright/.local/lib/python3.9/site-packages (from onnxruntime->rembg) (23.2) Collecting flatbuffers Downloading flatbuffers-24.3.25-py2.py3-none-any.whl (26 kB) Collecting protobuf Downloading protobuf-5.26.1-cp37-abi3-manylinux2014_x86_64.whl (302 kB) |████████████████████████████████| 302 kB 5.4 MB/s Requirement already satisfied: sympy in /home/jcartwright/.local/lib/python3.9/site-packages (from onnxruntime->rembg) (1.12) Collecting humanfriendly>=9.1 Downloading humanfriendly-10.0-py2.py3-none-any.whl (86 kB) |████████████████████████████████| 86 kB 886 kB/s |
Then use this simple script to remove the background from an image.
from rembg import remove from PIL import Image input_path = 'cousins.png'; output_path = 'output.png'; input = Image.open(input_path) output = remove(input) output.save(output_path) |
Save this as rem.py and then with a suitable image, perform the process.
(jcartwright@2403-4800-25af-b00--2) 192.168.1.5 Pictures $ python3 rem.py Downloading data from 'https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net.onnx' to file '/home/jcartwright/.u2net/u2net.onnx'. 100%|████████████████████████████████████████| 176M/176M [00:00<00:00, 609GB/s] |
This does work, this could easily be automated and many images could be processed with one script.