【批量去除图片的背景】

发布于:2023-01-16 ⋅ 阅读:(837) ⋅ 点赞:(0)

有一个需求,需要批量去除图片的背景,在网上搜到下面这个方法:【python】backgroundremover 去除背景_LiuPig刘皮哥的博客-CSDN博客GitHubhttps://github.com/nadermx/backgroundremoverBackgroundRemover 是一个命令行工具,用于从视频 和图像中删除背景,由nadermx 制作以支持https://BackgroundRemover.app。安装pip install backgroundremover使用import os# # C:\Users\dududada\.u2net\u2net_human_seg.pthos.system('back.https://blog.csdn.net/qq_27093891/article/details/122223018

这个不一定要在 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.新建文件夹

makir 归档_结果

现在生成了一个空目录 /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

搞定 

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到