Pixian の画像背景削除 API

Pixian.AI は、本格的な画像背景除去 API を提供しています。 API は、全自動で、クラス最高の忠実度をもって、画像から背景を除去します。

API キーを取得

クイックスタート

ビットマップ画像を POST して、背景が削除された結果を受け取ります。

$ curl https://api.pixian.ai/api/v2/remove-background \
 -u xyz123:[secret] \
 -F image=@example.jpeg \
 -o pixian_result.png
// Requires: org.apache.httpcomponents.client5:httpclient5-fluent

Request request = Request.post("https://api.pixian.ai/api/v2/remove-background")
   .addHeader("Authorization", "Basic cHh4YmJ6Yjk2bjJ3OGFtOltzZWNyZXRd")
   .body(
      MultipartEntityBuilder.create()
         .addBinaryBody("image", new File("example.jpeg")) // TODO: Replace with your image
         // TODO: Add more upload parameters here
         .build()
      );
ClassicHttpResponse response = (ClassicHttpResponse) request.execute().returnResponse();

if (response.getCode() == 200) {
   // Write result to disk, TODO: or wherever you'd like
   try (FileOutputStream out = new FileOutputStream("pixian_result.png")) {
      response.getEntity().writeTo(out);
   }
} else {
   System.out.println("Request Failed: Status: " + response.getCode() + ", Reason: " + response.getReasonPhrase());
}
using (var client = new HttpClient())
using (var form = new MultipartFormDataContent())
{
   client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "INSERT_API_KEY_HERE");
   form.Add(new ByteArrayContent(File.ReadAllBytes("example.jpeg")), "image", "example.jpeg"); // TODO: Replace with your image
   // TODO: Add more upload parameters here

   var response = client.PostAsync("https://api.pixian.ai/api/v2/remove-background", form).Result;

   if (response.IsSuccessStatusCode)
   {
      // Write result to disk, TODO: or wherever you'd like
      FileStream outStream = new FileStream("pixian_result.png", FileMode.Create, FileAccess.Write, FileShare.None);
      response.Content.CopyToAsync(outStream).ContinueWith((copyTask) => { outStream.Close(); });
   }
   else
   {
       Console.WriteLine("Request Failed: Status: " + response.StatusCode + ", Reason: " + response.ReasonPhrase);
   }
}
// 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.pixian.ai/api/v2/remove-background',
  formData: {
    image: fs.createReadStream('example.jpeg'), // TODO: Replace with your image
    // TODO: Add more upload options here
  },
  auth: {user: 'xyz123', pass: '[secret]'},
  followAllRedirects: true,
  encoding: null
}, function(error, response, body) {
  if (error) {
    console.error('Request failed:', error);
  } else if (!response || response.statusCode != 200) {
    console.error('Error:', response && response.statusCode, body.toString('utf8'));
  } else {
    // Save result
    fs.writeFileSync("pixian_result.png", body);
  }
});
$ch = curl_init('https://api.pixian.ai/api/v2/remove-background');

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
    array('Authorization: Basic cHh4YmJ6Yjk2bjJ3OGFtOltzZWNyZXRd'));
curl_setopt($ch, CURLOPT_POSTFIELDS,
    array(
      'image' => curl_file_create('example.jpeg'),
      // TODO: Add more upload options here
    ));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$data = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200) {
  // Save result
  file_put_contents("pixian_result.png", $data);
} else {
  echo "Error: " . $data;
}
curl_close($ch);
# Requires "requests" to be installed (see https://pypi.org/project/requests/)
import requests

response = requests.post(
    'https://api.pixian.ai/api/v2/remove-background',
    files={'image': open('example.jpeg', 'rb')},
    data={
        # TODO: Add more upload options here
    },
    auth=('xyz123', '[secret]')
)
if response.status_code == requests.codes.ok:
    # Save result
    with open('pixian_result.png', 'wb') as out:
        out.write(response.content)
else:
    print("Error:", response.status_code, response.text)
# Requires: gem install httpclient
require 'httpclient'

client = HTTPClient.new default_header: {
  "Authorization" => "Basic cHh4YmJ6Yjk2bjJ3OGFtOltzZWNyZXRd"
}

response = client.post("https://api.pixian.ai/api/v2/remove-background", {
  "image" => File.open("example.jpeg", "rb"), # TODO: Replace with your image
  # TODO: Add more upload parameters here
})

if response.status == 200 then
  # Write result to disk, TODO: or wherever you'd like
  File.open("pixian_result.png", 'w') { |file| file.write(response.body) }
else
  puts "Error: Code: " + response.status.to_s + ", Reason: " + response.reason
end
$ curl https://api.pixian.ai/api/v2/remove-background \
 -u xyz123:[secret] \
 -F 'image.url=https://example.com/example.jpeg' \
 -o pixian_result.png
// Requires: org.apache.httpcomponents.client5:httpclient5-fluent

Request request = Request.post("https://api.pixian.ai/api/v2/remove-background")
   .addHeader("Authorization", "Basic cHh4YmJ6Yjk2bjJ3OGFtOltzZWNyZXRd")
   .body(
      MultipartEntityBuilder.create()
         .addTextBody("image.url", "https://example.com/example.jpeg") // TODO: Replace with your image URL
         // TODO: Add more upload parameters here
         .build()
      );
ClassicHttpResponse response = (ClassicHttpResponse) request.execute().returnResponse();

if (response.getCode() == 200) {
   // Write result to disk, TODO: or wherever you'd like
   try (FileOutputStream out = new FileOutputStream("pixian_result.png")) {
      response.getEntity().writeTo(out);
   }
} else {
   System.out.println("Request Failed: Status: " + response.getCode() + ", Reason: " + response.getReasonPhrase());
}
using (var client = new HttpClient())
using (var form = new MultipartFormDataContent())
{
   client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "INSERT_API_KEY_HERE");
   form.Add(new StringContent("https://example.com/example.jpeg"), "image.url"); // TODO: Replace with your image URL
   // TODO: Add more upload parameters here

   var response = client.PostAsync("https://api.pixian.ai/api/v2/remove-background", form).Result;

   if (response.IsSuccessStatusCode)
   {
      // Write result to disk, TODO: or wherever you'd like
      FileStream outStream = new FileStream("pixian_result.png", FileMode.Create, FileAccess.Write, FileShare.None);
      response.Content.CopyToAsync(outStream).ContinueWith((copyTask) => { outStream.Close(); });
   }
   else
   {
       Console.WriteLine("Request Failed: Status: " + response.StatusCode + ", Reason: " + response.ReasonPhrase);
   }
}
// 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.pixian.ai/api/v2/remove-background',
  formData: {
    'image.url': 'https://example.com/example.jpeg', // TODO: Replace with your image
    // TODO: Add more upload options here
  },
  auth: {user: 'xyz123', pass: '[secret]'},
  followAllRedirects: true,
  encoding: null
}, function(error, response, body) {
  if (error) {
    console.error('Request failed:', error);
  } else if (!response || response.statusCode != 200) {
    console.error('Error:', response && response.statusCode, body.toString('utf8'));
  } else {
    // Save result
    fs.writeFileSync("pixian_result.png", body);
  }
});
$ch = curl_init('https://api.pixian.ai/api/v2/remove-background');

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
    array('Authorization: Basic cHh4YmJ6Yjk2bjJ3OGFtOltzZWNyZXRd'));
curl_setopt($ch, CURLOPT_POSTFIELDS,
    array(
      'image.url' => 'https://example.com/example.jpeg',
      // TODO: Add more upload options here
    ));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$data = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200) {
  // Save result
  file_put_contents("pixian_result.png", $data);
} else {
  echo "Error: " . $data;
}
curl_close($ch);
# Requires "requests" to be installed (see https://pypi.org/project/requests/)
import requests

response = requests.post(
    'https://api.pixian.ai/api/v2/remove-background',
    data={
        'image.url': 'https://example.com/example.jpeg',
        # TODO: Add more upload options here
    },
    auth=('xyz123', '[secret]')
)
if response.status_code == requests.codes.ok:
    # Save result
    with open('pixian_result.png', 'wb') as out:
        out.write(response.content)
else:
    print("Error:", response.status_code, response.text)
# Requires: gem install httpclient
require 'httpclient'

client = HTTPClient.new default_header: {
  "Authorization" => "Basic cHh4YmJ6Yjk2bjJ3OGFtOltzZWNyZXRd"
}

response = client.post("https://api.pixian.ai/api/v2/remove-background", {
  "image.url" => "https://example.com/example.jpeg", # TODO: Replace with your image URL
  # TODO: Add more upload parameters here
})

if response.status == 200 then
  # Write result to disk, TODO: or wherever you'd like
  File.open("pixian_result.png", 'w') { |file| file.write(response.body) }
else
  puts "Error: Code: " + response.status.to_s + ", Reason: " + response.reason
end

他のプロバイダーからの移行ですか? Check out our migration guide

価格

API の統合をテストするのは無料です。購入する必要はありません。

開発には、test=trueを使ってください。 最初のページのインタラクティブウェブアプリを使って、結果の品質を評価することができます。

運用版の結果には、クレジットパックの購入が必要になります。 価格ページをご覧ください。

認証およびセキュリティ

API は、標準の HTTP 基本アクセス認証を使用します。 API に対するすべてのリクエストは、HTTPS で行い、API Id をユーザーとして、API シークレットをパスワードとする API 資格情報を含める必要があります。

リクエストを正しく行うには、http クライアントライブラリは、サーバー名表示(SNI) に対応している必要があります。 おかしなハンドシェークエラーが発生する場合、大抵これが原因となっています。

帯域制限

API の利用にはレート制限がありますが、割当量には余裕があり、固定された上限値があるわけではありません。

通常のエンドユーザー操作では使用方法に緩急があるのが常なので、ユーザー側で帯域制限を意識することはなく、サービスは円滑に処理されます。

ただし、バッチジョブでは最大 5 スレッドから始め、お望みの並列処理数に達するまで 5 分ごとに 1 新規スレッドを追加することをお奨めします。 100 同時スレッドを超える処理を必要とする場合は、作業開始前にご連絡ください。

あまりにも多くのリクエストを提出すると、429 Too Many Requestsのレスポンスが返されます。 これが発生したら、リニアバックオフを適用してください。すなわち、最初のレスポンスが返った後、次のリクエストの提出まで 5 秒間待ちます。 引き続き 2 回目の 429 レスポンス発生の場合、次のリクエストの提出まで 2*5=10 秒間待ちます。 3 回目は、 3*5=15 秒待ちます(以下同様)。

バックオフカウンターは、リクエストが正しく完了した後リセットできます。バックオフはスレッドごとに適用してください(すなわち、スレッドは相互に独立に操作する)。

タイムアウト

API リクエストは通常数秒以内に完了しますが、一時的に過負荷状況が発生すると、より長い処理時間が必要になる可能性があります。

クライアントライブラリが、API リクエストを早期終了しないよう、これはアイドルタイムアウトを 180 秒以上とするように設定すべきです。

エラー JSON オブジェクト

当社は、API リクエストの成功または失敗を示すのに、従来の HTTP ステータスを使用し、返されるエラー JSON オブジェクトに重要なエラー情報を含めています。

当社は、問題のあるリクエストには、必ずエラー JSON オブジェクトを返すように努めています。 しかしながら、理論的には常に、内部サーバーの障害により、JSON 以外のエラーレスポンスが返される可能性があります。

属性

status応答の HTTP ステータスをここに再掲されるのでデバッグに役立ててください。
codePixian.AI 内部エラーコード。
message人間が読みやすいエラーメッセージ、デバッグに役立ててください。

リクエストに対する HTTP ステータスが 200 の場合、JSON オブジェクトが返されず、リクエストは概して言えば正しく処理されたと想定することができます。

HTTP クライアントライブラリによっては、HTTP ステータスの例外を 400599 の範囲で上げてきます。 それら例外を捕捉して、適切に処理する必要があります。

HTTP Status意味
200-299

成功

400-499

リクエストで提供された情報に問題があります(パラメータの欠如など)。 エラーメッセージを確認して、修正方法を考えてください。

500-599

Pixian.AI 内部エラーが発生しました。 少し待ってやり直してください。問題が続くようでしたら、メールでお問い合わせください。

エラー応答の例

{
  "error" : {
    "status" : 400,
    "code" : 1006,
    "message" : "Failed to read the supplied image. "
  }
}

差分 PNG 形式 低遅延、低帯域

Pixian.AI は、差分 PNG 出力形式を提供する最初の背景削除サービスであることを嬉しく思っております。 差分 PNG は迅速に符号化でき、通常の PNG よりはるかに小さくすることができます。 したがって、モバイルアプリなどの遅延や帯域に厳しいアプリケーションに最適な形式となります。

もっと読む 右の矢印

背景を削除 POST
https://api.pixian.ai/api/v2/remove-background

画像から背景を削除するには、標準 HTTP POST ファイルのアップロードを行います。 コンテンツタイプ は、multipart/form-data でなければならないことに注意してください。

パラメータ

入力画像は、以下のいずれかでなければなりません。


バイナリ

バイナリファイル。


文字列

base64 でエンコードされた文字列。 文字列のサイズは、最大 1 メガバイトです。


文字列

取り込み処理するための URL

.bmp、.gif、.jpeg、.png または .tiff ファイルでなければなりません。

最大画像アップロードサイズ(= 幅 × 高さ)は、32,000,000 ピクセルで、max_pixels に圧縮されます。


ブール型、デフォルト:false

これが試験用画像であることを示すため、trueを渡します。

実際の画像向けには、省略するか、falseを渡してください。

試験画像は、自由に処理できますが、結果にはウォーターマークが組み込まれます。


整数、10025000000、デフォルト:25000000

最大入力画像サイズ(= 幅 × 高さ)。 これより大きな画像は、処理前にこのサイズに圧縮されます。


形式:「#RRGGBB」、例:#0055FF

結果に適用する背景色。 透明の背景を残す場合は省略します。

「#」のプレフィックスを含めることを忘れないでください。


ブール型、デフォルト:false

結果を前景の対象物に合わせてトリミングするかどうか.

result.margin および result.target_size と共に使用すると、いつでも対象物が適切なサイズで中心に配置されるなど良好な結果を得ることができ非常に便利です。


形式:「(x.y%|px){1,4}」、例:10px 20% 5px 15%、デフォルト:0px

結果に追加される余白。

結果が前景に対しトリミングされているかどうかにかかわらず、余白が追加されます。

result.target_size が指定されている場合、余白が追加されます。すなわち、実質的なターゲットサイズは拡張されません。

対応している単位は、% および px です。 これは CSS の文法に従うので、以下のいずれを使用することもできます。

  • [all]
  • [top/bottom] [left/right]
  • [top] [left/right] [bottom]
  • [top] [right] [bottom] [left]


形式:「w h」、例:1920 1080

特定の結果のサイズをピクセルで指定します。 結果は、指定サイズ内に合わせて拡大・縮小されます。 余剰の空白がある場合、常に水平に中央配置され、垂直方向の取り扱いは、result.vertical_alignment によって制御されます。


列挙型、デフォルト:middle

result.target_size が使用された場合、垂直方向の余剰空白の配置方法を指定します。


列挙型、デフォルト:auto

出力形式。 auto は、background.color が指定された場合、透明の結果については png と解釈され、不透明の結果については jpeg と解釈されます。

delta_png は、モバイルアプリなどの低遅延かつ帯域制限のある場合において特に有効な、高度、拘束かつ非常にコンパクトな形式です。 これは、背景を透明な黒 0x00000000 そして前景を透明な白 0x00FFFFFF と符号化します。 部分的に透明なピクセルは、実際の色の値となります。 これは、入力画像と共に完全な結果を再構築するために使用されます。 Learn more about the Delta PNG format

delta_png が使用される場合、background.colorresult.crop_to_foregroundresult.marginresult.target_size および result.vertical_alignment は無視されます。 結果は、入力画像と同じサイズでなければなりません。そうでないと復号化が失敗します。したがって、max_pixels によって入力が縮小されることがあってはなりません。


整数、1100、デフォルト:75

JPEG 結果を符号化する場合に使用される品質。

結果ヘッダー

X-Credits-Charged 実際に課金されたクレジット。
X-Credits-Calculated これが運用版でのリクエストだった場合は課金されたであろう計算上のクレジット。 テスト版のリクエストのみに返されます。
X-Input-Orientation 入力画像から読み込まれ、入力画像に適用される EXIF Orientation タグ。 これは、1 ~ 8 の整数値です(1 および 8 を含む)。 これは、画像読み込みライブラリが EXIF Orientation に対応していない場合に有益です。 Read more about EXIF orientation here
X-Input-Size サイズ制限適用前のピクセル表示による入力画像の [width] [height]
X-Result-Size ピクセル表示による結果画像の [width] [height]
X-Input-Foreground 入力画像座標における前景の [top] [left] [width] [height] 境界ボックス。
X-Result-Foreground 結果画像座標における前景の [top] [left] [width] [height] 境界ボックス。

API 変更ログ

日付変更
2024/03/04 タイムアウトに関するセクションを追加。
2024/01/16 エラー JSON オブジェクトを文書化しました。
2024/01/11 現在実際に返されているのは、X-Credits-Charged ヘッダーですが、テスト版のリクエストのために X-Credits-CalculatedVヘッダーが追加されました。
2023/06/13 API をバージョン 2 に更新。また、読みやすさのため、パラメータを camelCase から snake-case に更新。 前バージョンの API は廃止予定ですが、まだ利用可能です。
2023/05/03 API パラメータを大幅に拡張。
2023/04/28 API エンドポイントを更新。
2023/03/21 初回のリリース。
API キーを取得