【批量去除图片的背景】

有一个需求,需要批量去除图片的背景,在网上搜到下面这个方法:【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.新建文件夹

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

搞定 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
在Python中,使用OpenCV库可以实现批量去除图片背景的功能。下面是一种常见的方法: 1. 导入所需的库: ```python import cv2 import numpy as np ``` 2. 定义一个函数来去除图片背景: ```python def remove_background(image_path): # 读取图片 image = cv2.imread(image_path) # 创建一个与图片大小相同的掩膜 mask = np.zeros(image.shape[:2], np.uint8) # 定义前景和背景模型 bgdModel = np.zeros((1,65),np.float64) fgdModel = np.zeros((1,65),np.float64) # 定义矩形区域,包含前景目标 rect = (50, 50, image.shape[1]-50, image.shape[0]-50) # 使用GrabCut算法进行图像分割 cv2.grabCut(image, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT) # 创建一个掩膜,将确定或可能是前景的区域设置为1,其他区域设置为0 mask2 = np.where((mask==2)|(mask==0), 0, 1).astype('uint8') # 将原始图像与掩膜相乘,去除背景 result = image * mask2[:, :, np.newaxis] return result ``` 3. 批量处理图片: ```python def batch_remove_background(image_folder): import os # 获取文件夹中的所有图片文件 image_files = [f for f in os.listdir(image_folder) if f.endswith('.jpg') or f.endswith('.png')] # 遍历每个图片文件,进行背景去除 for image_file in image_files: image_path = os.path.join(image_folder, image_file) result = remove_background(image_path) # 保存去除背景后的图片 result_path = os.path.join(image_folder, 'result_' + image_file) cv2.imwrite(result_path, result) ``` 使用上述代码,你可以将需要处理的图片放在同一个文件夹中,然后调用`batch_remove_background`函数进行批量去除背景

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值