Using Python to Remove Image Backgrounds
A couple of weeks ago, I saw a notification containing a simple Python script that would remove backgrounds from images. The sample source code was something like this:
from rembg import remove
from PIL import Image
inp = Image.open('image.jpg')
output = remove(inp)
output.save('image.png')
I tried it. The results were pretty impressive. Without providing any parameters to speak of, a number of images that I used to test the library were transformed into images highlighting a central person or object while rendering the background transparent.
Before
After
Installation
To install the library, I used the following pip command:
pip install rembg
The library seemed to install correctly under Windows 10. I then tried to run the example script above. I received the following error:
Too many users have viewed or downloaded this file recently. Please
try accessing the file again later. If the file you are trying to
access is particularly large or is shared with many people, it may
take up to 24 hours to be able to view or download the file. If you
still can't access a file after 24 hours, contact your domain
administrator.
To remedy the situation, you can optionally download the necessary file u2net.onnx using a browser at the following URL:
https://drive.google.com/uc?id=1tCU5MM1LhRgGou5OpmpjBQbSrYIUoYab
Under Windows, you can then copy the u2net.onnx file to %userprofile%\.u2net\ where %userprofile% is the current user’s home directory. You might have to manually perform a similar procedure under MacOS and/or Linux.
I found the above directions at the python-catlin blog here:
https://python-catalin.blogspot.com/2022/10/python-3107-rembg-for-remove-background.html
An Improved Script
I wanted to run the transforms against various images that I had in electronic photo albums just to see what the transformed output would look like. I didn’t want to have to substitute the hard-coded filenames that were used in the original snippet. I came up with the script nobg.py:
# Copyright (c) 2022 by James K. Lawless
# jimbo@radiks.net
# License: MIT / X11
# See: http://jimlawless.net/license2022.phps
# for full license details.
import argparse
from rembg import remove
from PIL import Image
if __name__ == "__main__":
parser=argparse.ArgumentParser()
parser.description="Remove background from an image file"
parser.add_argument("-infile",required=True)
parser.add_argument("-outfile",required=True)
args=parser.parse_args()
print("Converting",args.infile,"to",args.outfile)
inp = Image.open(args.infile)
output = remove(inp)
output.save(args.outfile)
The source code will be maintained at: https://github.com/jimlawless/nobg
To remove the background from the file image.jpg leaving the result in the file image.png, we would use the following command line:
python nobg.py -infile image.jpg -outfile image.png
On the console, you should see the message:
Converting image.jpg to image.png
I then wrote a Windows batch file to iterate over a directory of images with the extention “jpeg” or “jpg” converting each to a like-named “png” file:
nobg_all.bat
@echo off
for %%x in (*.j*) do python nobg.py -infile %%x -outfile %%~nx.png
You might have to add some path information if nobg.py isn’t in the current directory.
I’ll have to dig into this a bit more as time permits. I hope you have fun with the rembg library!