Package: rembg
RembgRembg is a tool to remove images background.If this project has helped you, please consider making adonation.SponsorPhotoRoom Remove Background APIhttps://photoroom.com/apiFast and accurate background remover APIRequirementspython: >3.7, <3.13InstallationCPU support:pipinstallrembg# for librarypipinstallrembg[cli]# for library + cliGPU support:First of all, you need to check if your system supports theonnxruntime-gpu.Go tohttps://onnxruntime.aiand check the installation matrix.If yes, just run:pipinstallrembg[gpu]# for librarypipinstallrembg[gpu,cli]# for library + cliUsage as a cliAfter the installation step you can use rembg just typingrembgin your terminal window.Therembgcommand has 4 subcommands, one for each input type:ifor filespfor folderssfor http serverbfor RGB24 pixel binary streamYou can get help about the main command using:rembg --helpAs well, about all the subcommands using:rembg <COMMAND> --helprembgiUsed when input and output are files.Remove the background from a remote imagecurl -s http://input.png | rembg i > output.pngRemove the background from a local filerembg i path/to/input.png path/to/output.pngRemove the background specifying a modelrembg i -m u2netp path/to/input.png path/to/output.pngRemove the background returning only the maskrembg i -om path/to/input.png path/to/output.pngRemove the background applying an alpha mattingrembg i -a path/to/input.png path/to/output.pngPassing extras parametersSAM example

rembg i -m sam -x '{ "sam_prompt": [{"type": "point", "data": [724, 740], "label": 1}] }' examples/plants-1.jpg examples/plants-1.out.pngCustom model example

rembg i -m u2net_custom -x '{"model_path": "~/.u2net/u2net.onnx"}' path/to/input.png path/to/output.pngrembgpUsed when input and output are folders.Remove the background from all images in a folderrembg p path/to/input path/to/outputSame as before, but watching for new/changed files to processrembg p -w path/to/input path/to/outputrembgsUsed to start http server.rembg s --host 0.0.0.0 --port 7000 --log_level infoTo see the complete endpoints documentation, go to:http://localhost:7000/api.Remove the background from an image urlcurl -s "http://localhost:7000/api/remove?url=http://input.png" -o output.pngRemove the background from an uploaded imagecurl -s -F file=@/path/to/input.jpg "http://localhost:7000/api/remove"  -o output.pngrembgbProcess a sequence of RGB24 images from stdin. This is intended to be used with another program, such as FFMPEG, that outputs RGB24 pixel data to stdout, which is piped into the stdin of this program, although nothing prevents you from manually typing in images at stdin.rembg b image_width image_height -o output_specifierArguments:image_width : width of input image(s)image_height : height of input image(s)output_specifier: printf-style specifier for output filenames, for example ifoutput-%03u.png, then output files will be namedoutput-000.png,output-001.png,output-002.png, etc. Output files will be saved in PNG format regardless of the extension specified. You can omit it to write results to stdout.Example usage with FFMPEG:ffmpeg -i input.mp4 -ss 10 -an -f rawvideo -pix_fmt rgb24 pipe:1 | rembg b 1280 720 -o folder/output-%03u.pngThe width and height values must match the dimension of output images from FFMPEG. Note for FFMPEG, the "-an -f rawvideo -pix_fmt rgb24 pipe:1" part is required for the whole thing to work.Usage as a libraryInput and output as bytesfromrembgimportremoveinput_path='input.png'output_path='output.png'withopen(input_path,'rb')asi:withopen(output_path,'wb')aso:input=i.read()output=remove(input)o.write(output)Input and output as a PIL imagefromrembgimportremovefromPILimportImageinput_path='input.png'output_path='output.png'input=Image.open(input_path)output=remove(input)output.save(output_path)Input and output as a numpy arrayfromrembgimportremoveimportcv2input_path='input.png'output_path='output.png'input=cv2.imread(input_path)output=remove(input)cv2.imwrite(output_path,output)How to iterate over files in a performatic wayfrompathlibimportPathfromrembgimportremove,new_sessionsession=new_session()forfileinPath('path/to/folder').glob('*.png'):input_path=str(file)output_path=str(file.parent/(file.stem+".out.png"))withopen(input_path,'rb')asi:withopen(output_path,'wb')aso:input=i.read()output=remove(input,session=session)o.write(output)To see a full list of examples on how to use rembg, go to theexamplespage.Usage as a dockerJust replace therembgcommand fordocker run danielgatis/rembg.Try this:docker run -v path/to/input:/rembg danielgatis/rembg i input.png path/to/output/output.pngModelsAll models are downloaded and saved in the user home folder in the.u2netdirectory.The available models are:u2net (download,source): A pre-trained model for general use cases.u2netp (download,source): A lightweight version of u2net model.u2net_human_seg (download,source): A pre-trained model for human segmentation.u2net_cloth_seg (download,source): A pre-trained model for Cloths Parsing from human portrait. Here clothes are parsed into 3 category: Upper body, Lower body and Full body.silueta (download,source): Same as u2net but the size is reduced to 43Mb.isnet-general-use (download,source): A new pre-trained model for general use cases.isnet-anime (download,source): A high-accuracy segmentation for anime character.sam (download encoder,download decoder,source): A pre-trained model for any use cases.How to train your own modelIf You need more fine tuned models try this:https://github.com/danielgatis/rembg/issues/193#issuecomment-1055534289Some video tutorialshttps://www.youtube.com/watch?v=3xqwpXjxyMQhttps://www.youtube.com/watch?v=dFKRGXdkGJUhttps://www.youtube.com/watch?v=Ai-BS_T7yjEhttps://www.youtube.com/watch?v=D7W-C0urVcQReferenceshttps://arxiv.org/pdf/2005.09007.pdfhttps://github.com/NathanUA/U-2-Nethttps://github.com/pymatting/pymattingFAQWhen will this library provide support for Python version 3.xx?This library directly depends on theonnxruntimelibrary. Therefore, we can only update the Python version whenonnxruntimeprovides support for that specific version.Buy me a coffeeLiked some of my work? Buy me a coffee (or more likely a beer)LicenseCopyright (c) 2020-presentDaniel GatisLicensed underMIT License