技术频道


5 秒自动抠图

5 秒去除人像背景 —— AI 帮你自动抠图,无需注册登录、完全免费!

Remove Image Background 是非常火爆的人像照片(其实也可以去除其他物体,比如沙发…)背景去除工具,只需要上传一张带有人像的照片,它就会帮你自动抠掉背景,整个过程全自动、5秒内完成,而且完全免费,甚至还支持多种语言的 Background Removal API 调用。

效果如下图所示,不到5秒瞬间完成…(该图为本人真实、亲测旅游卫视图片处理效果)

图1 - 亲测 RemoveBg 旅游卫视单人或者任何物体
图2 - 亲测 RemoveBg 旅游卫视双人自动API识别 5 秒抠图
Remove Bg 传送门 Removal API 传送门

另外,再推荐一个国内在线的抠图工具 —— 搞定抠图,功能相比 Remove Bg 稍微逊色,主要不够 AI,很多需要人工确定抠图范围,在访问 Remove Bg 遇到困难的时候(remove.bg 使用了 reCAPTCHA 预防滥用,而 re 的服务器位于 Google,所以你懂得 ..),也可以作为备选使用:

搞定抠图 传送门

Background Removal API Community packages


懂开发的攻城狮同学,也可以根据下面实现好的,直接拿来改装,不用自己造重复轮子。

Background Removal API 各开发语言实例


  • cURL

$ curl -H 'X-API-Key: INSERT_YOUR_API_KEY_HERE'           \
       -F 'image_url=https://commandnotfound.cn/example.jpg'   \
       -F 'size=auto'                                     \
       -f https://api.remove.bg/v1.0/removebg -o no-bg.png

  • Node.js

// Requires "request" to be installed (see https://www.npmjs.com/package/request)
var request = require('request');
var fs = require('fs');

request.post({
  url: 'https://api.remove.bg/v1.0/removebg',
  formData: {
    image_url: 'https://commandnotfound.cn/example.jpg',
    size: 'auto',
  },
  headers: {
    'X-Api-Key': 'INSERT_YOUR_API_KEY_HERE'
  },
  encoding: null
}, function(error, response, body) {
  if(error) return console.error('Request failed:', error);
  if(response.statusCode != 200) return console.error('Error:', response.statusCode, body.toString('utf8'));
  fs.writeFileSync("no-bg.png", body);
});

  • Python

# Requires "requests" to be installed (see python-requests.org)
import requests

response = requests.post(
    'https://api.remove.bg/v1.0/removebg',
    data={
        'image_url': 'https://commandnotfound.cn/example.jpg',
        'size': 'auto'
    },
    headers={'X-Api-Key': 'INSERT_YOUR_API_KEY_HERE'},
)
if response.status_code == requests.codes.ok:
    with open('no-bg.png', 'wb') as out:
        out.write(response.content)
else:
    print("Error:", response.status_code, response.text)

  • Ruby

# Install "remove_bg" first (https://github.com/remove-bg/ruby)
require "remove_bg"

RemoveBg.from_url("https://commandnotfound.cn/example.jpg",
  api_key: "INSERT_YOUR_API_KEY_HERE"
).save("no-bg.png")

  • PHP

// Requires "guzzle" to be installed (see guzzlephp.org)

$client = new GuzzleHttp\Client();
$res = $client->post('https://api.remove.bg/v1.0/removebg', [
    'multipart' => [
        [
            'name'     => 'image_url',
            'contents' => 'https://commandnotfound.cn/example.jpg'
        ],
        [
            'name'     => 'size',
            'contents' => 'auto'
        ]
    ],
    'headers' => [
        'X-Api-Key' => 'INSERT_YOUR_API_KEY_HERE'
    ]
]);

$fp = fopen("no-bg.png", "wb");
fwrite($fp, $res->getBody());
fclose($fp);

  • Java

// Requires "Apache HttpComponents" to be installed (see hc.apache.org)

Response response = Request.Post("https://api.remove.bg/v1.0/removebg")
    .addHeader("X-Api-Key", "INSERT_YOUR_API_KEY_HERE")
    .body(
        MultipartEntityBuilder.create()
        .addTextBody("image_url", "https://commandnotfound.cn/example.jpg")
        .addTextBody("size", "auto")
        .build()
    ).execute();
response.saveContent(new File("no-bg.png"));

  • .NET (C#)

using (var client = new HttpClient())
using (var formData = new MultipartFormDataContent())
{
    formData.Headers.Add("X-Api-Key", "INSERT_YOUR_API_KEY_HERE");
    formData.Add(new StringContent("https://commandnotfound.cn/example.jpg"), "image_url");
    formData.Add(new StringContent("auto"), "size");
    var response = client.PostAsync("https://api.remove.bg/v1.0/removebg", formData).Result;

    if(response.IsSuccessStatusCode) {
        FileStream fileStream = new FileStream("no-bg.png", FileMode.Create, FileAccess.Write, FileShare.None);
        response.Content.CopyToAsync(fileStream).ContinueWith((copyTask) =>{ fileStream.Close(); });
    } else {
        Console.WriteLine("Error: " + response.Content.ReadAsStringAsync().Result);
    }
}

  • Swift

// Requires Alamofire to be installed (see https://github.com/Alamofire/Alamofire)

Alamofire
    .request(
        URL(string: "https://api.remove.bg/v1.0/removebg")!,
        method: .post,
        parameters: ["image_url": "https://commandnotfound.cn/example.jpg"],
        encoding: URLEncoding(),
        headers: [
            "X-Api-Key": "INSERT_YOUR_API_KEY_HERE"
        ]
    )
    .responseData { imageResponse in
        guard let imageData = imageResponse.data,
            let image = UIImage(data: imageData) else { return }

        self.imageView.image = image
    }

  • Object-C

// Requires AFNetworking to be installed (see https://github.com/AFNetworking/AFNetworking)

AFHTTPSessionManager *manager =
[[AFHTTPSessionManager alloc] initWithSessionConfiguration:
 NSURLSessionConfiguration.defaultSessionConfiguration];

manager.responseSerializer = [AFImageResponseSerializer serializer];
[manager.requestSerializer setValue:@"INSERT_YOUR_API_KEY_HERE"
                 forHTTPHeaderField:@"X-Api-Key"];

NSURLSessionDataTask *dataTask = [manager
                                  POST:@"https://api.remove.bg/v1.0/removebg"
                                  parameters:@{@"image_url": @"https://commandnotfound.cn/example.jpg"}
                                  progress:nil
                                  success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                                      if ([responseObject isKindOfClass:UIImage.class] == false) {
                                          return;
                                      }

                                      self.imageView.image = responseObject;
                                  } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                                      NSLog(@"ERROR");
                                  }];
[dataTask resume];

发表评论