文章目录方法一、基于removebg的背景替换1.1、removebg介绍1.2、环境配置1.3、算法详解1.4、实战单张图片背景替换1.5、实战多张图片背景替换1.6、实战UI自选择图片进行背景替换方法二、基于backgroundremover的背景替换2.1、backgroundremover介绍2.2、环境配置2.2.1、Image命令行实现2.2.2、Video命令行实现方法一、基于removebg的背景替换1.1、removebg介绍removebg官网一款在线AI自动抠图免费工具桌面版APP需注册账号removebg网站提供了API 接口可以直接调用并实现抠图。图片必须有一个明显是前景的主题例如人、动物、产品或汽车更多信息。使用API or APP每个月只有50张图片的免费处理权限而网站的使用无限制且全免费。免费处理权限只支持最高 25 万像素的小分辨率图片如625 × 400 像素想要获得高像素抠图则需要花费credit。用完之后就会拒绝你的API请求。支持上传最大12 兆字节的任何JPG 或 PNG 图像。如果图像分辨率大于25 兆像素则会将其调整为该最大分辨率。可以通过 API 同时处理500 张图像。1.2、环境配置详细过程如下1安装removebg包pip install removebg离线安装包下载Links for removebg2Get API Key获取 API Key 的详细过程如下:-11、removebg官网-Pricing-Sign up free-邮箱注册与账号激活。-22、登录-My Account-APP-Keys-New API Key-Create API Key-Copy API Key-Save changes-my API Key2BBwNqN61oZpaPqKmiF2ZGey1.3、算法详解1调用API接口RemoveBg函数完成图片抠图函数说明rmbgRemoveBg(API_Key,error.log)输入参数 API_Key 字符串格式error.log固定格式2调用remove_background_from_img_file函数完成背景替换。函数说明rmbg.remove_background_from_img_file(img_file_path,size,bg_color)输入参数 img_file_path 原始图片的路径 size 输出图片的质量regular0.25MP默认hd4MP4kup to 10MP。 bg_color 输出图片的背景如green 输出参数 自动保存结果在输入路径1.4、实战单张图片背景替换fromremovebgimportRemoveBg rmbgRemoveBg(YOUR-API-KEY,error.log)rmbg.remove_background_from_img_file(photo.jpg)# 输出图片4k分辨率白色背景1.5、实战多张图片背景替换fromremovebgimportRemoveBgimportos rmbgRemoveBg(YOUR-API-KEY,error.log)path%s/picture%os.getcwd()# 将生成结果放在当前路径的picture文件夹中# os.getcwd() 获取当前工作路径# os.listdir()返回指定的文件夹包含的文件或文件夹的名字的列表。forpicinos.listdir(path):rmbg.remove_background_from_img_file(%s\%s%(path,pic))removebg包一键抠图换证件照底色Python教程去除背景(附多种方式)python代码的打包.exe与自定义安装1.6、实战UI自选择图片进行背景替换fromremovebgimportRemoveBgimportwin32ui dlgwin32ui.CreateFileDialog(1)# 1表示打开文件对话框dlg.SetOFNInitialDir(E:/图片)# 设置打开文件对话框中的初始显示目录dlg.DoModal()filenamedlg.GetPathName()# 获取选择的文件名称rmbgRemoveBg(kRSbRHJZePGWnEBfKeqFbtSz,error.log)# 引号内是你获取的APIrmbg.remove_background_from_img_file(filename)# 图片地址方法二、基于backgroundremover的背景替换2.1、backgroundremover介绍BackgroundRemover是一个命令行工具从图像和视频中删除背景。github源代码BackgroundRemover在线使用BackgroundRemover.app2.2、环境配置安装python - backgroundremover包pip install backgroundremover图像或视频的使用方式通过命令行使用2.2.1、Image命令行实现Remove the background from a local file imagebackgroundremover-i/path/to/image.jpeg-ooutput.png图像的高级用法1Sometimes it is possible to achieve better results by turning on alpha matting.backgroundremover-i/path/to/image.jpeg-a-ae15-ooutput.png2change the model for diferent background removal methods between u2netp, u2net, or u2net_human_segbackgroundremover-i/path/to/image.jpeg-mu2net_human_seg-ooutput.png# 通过Pycharm调用importos os.system(backgroundremover -i cg.jpg -o cg_output.jpg)2.2.2、Video命令行实现1remove background from video and make transparent movbackgroundremover-i/path/to/video.mp4-tv-ooutput.mov2remove background from local video and overlay it over other videobackgroundremover-i/path/to/video.mp4-tov/path/to/videtobeoverlayed.mp4-ooutput.mov3remove background from local video and overlay it over an imagebackgroundremover-i/path/to/video.mp4-toi/path/to/videtobeoverlayed.mp4-ooutput.mov4remove background from video and make transparent gifbackgroundremover-i/path/to/video.mp4-tg-ooutput.gif5Make matte key file (green screen overlay).Make a matte file for premierbackgroundremover-i/path/to/video.mp4-mk-ooutput.matte.mp4视频的高级用法1Change the framerate of the video (default is set to 30)backgroundremover-i/path/to/video.mp4-fr30-tv-ooutput.mov2Set total number of frames of the video (default is set to -1, ie the remove background from full video)backgroundremover-i/path/to/video.mp4-fl150-tv-ooutput.mov3Change the gpu batch size of the video (default is set to 1)backgroundremover-i/path/to/video.mp4-gb4-tv-ooutput.mov4Change the number of workers working on video (default is set to 1)backgroundremover-i/path/to/video.mp4-wn4-tv-ooutput.mov5change the model for diferent background removal methods between u2netp, u2net, or u2net_human_seg and limit the frames to 150backgroundremover-i/path/to/video.mp4-mu2net_human_seg-fl150-tv-ooutput.mov