这个不一定要在 python 的环境运行,用 shell 命令直接运行即可
backgroundremover -i " /path/to/image.jpeg " -o " output.png "
如果没有科学上网,需要下载预训练包,下载地址:
u2net.pth 下载地址https://github.com/OPHoperHPO/image-background-remove-tool/releases/download/3.2/u2net.pth然后放到合适的位置,比如 /Users/myname/.u2net/
但是,这个只能去除一张照片,现在写个脚本,以便批量处理:
#!/bin/bash
find ./归档 -name "*.JPG" > in.txt
find ./归档 -name "*.JPG" | awk -vb=./归档_结果/ -F '[ /]' '{jpgOut=b$3;print jpgOut}' > out.txt
exec 3<"in.txt"
exec 4<"out.txt"
while read line1<&3 && read line2<&4
do
echo $line1 $line2
backgroundremover -i $line1 -o $line2
done
假设我现在需要批量处理的图片的路径是 /Users/Downloads/tmp/归档
那么 cd 到 /Users/Downloads/tmp,然后:
1.新建文件夹
mkdir 归档_结果
现在生成了一个空目录 /Users/Downloads/tmp/归档_结果,生成的图片将保存在这里面
2.新建脚本
vim doit.sh
打开脚本后输入以下内容
#!/bin/bash
find ./归档 -name "*.JPG" > in.txt
find ./归档 -name "*.JPG" | awk -vb=./归档_结果/ -F '[ /]' '{jpgOut=b$3;print jpgOut}' > out.txt
exec 3<"in.txt"
exec 4<"out.txt"
while read line1<&3 && read line2<&4
do
echo $line1 $line2
backgroundremover -i $line1 -o $line2
done
保存退出后执行:
sh doit.sh
搞定