> ## Documentation Index
> Fetch the complete documentation index at: https://docs.jiekou.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# GPT Image

パラメータと使用方法は公式仕様に準拠しています。詳細なパラメータについては、公式ドキュメントを直接参照してください。

## 呼び出し方法

* `https://{api_domain}/openai/v1/images/generations`
* `https://{api_domain}/openai/v1/images/edits`

BaseURL：

* `https://{api_domain}/openai/v1`

## 対応モデル

* `gpt-image-2`
* `gpt-image-2-oai`
* `gpt-image-2.5-sunburst-oai`
* `gpt-image-2.5-flare-oai`

## 説明

このインターフェースAPIはOpenAI Image APIと互換性があり、現在は画像生成と画像編集に対応しています。詳細なパラメータについては、OpenAI公式ドキュメントを参照してください：

* [OpenAI 公式画像生成ガイド](https://developers.openai.com/api/docs/guides/image-generation)

* [Images generate](https://developers.openai.com/api/reference/resources/images/methods/generate)

* [Images edit](https://developers.openai.com/api/reference/resources/images/methods/edit)

* 画像生成リクエストは `application/json` を使用します。

* 画像編集は `multipart/form-data` ファイルアップロードに対応しており、`application/json` と `images` 配列を使用した画像参照の受け渡しにも対応しています。以下にJSONサンプルを提供します。

* `size` と `quality` を明示的に設定し、画像品質とコストを制御することを推奨します。料金は実際のトークン使用量とモデル価格に基づいて計算され、使用量はレスポンスの `usage` フィールドで確認できます。

## モデルとパラメータ

* 詳細な画像編集には `gpt-image-2.5-sunburst-oai` を、高速な画像生成には `gpt-image-2.5-flare-oai` を使用します。リクエスト内の `model` は、上記の完全なモデル名を使用してください。
* 2.5 の `quality`：`low` / `medium` / `high` / `xhigh` / `max` / `auto`、デフォルトは `auto`；GPT Image 2 は新しく追加された `xhigh` / `max` に対応していません。
* 透明な背景の画像を生成するには、`background="transparent"` を設定し、`output_format="png"` または `"webp"` を使用します。
* `size` で画像サイズを設定します。例：`1024x1024`、`1536x1024`、`1024x1536`。カスタムサイズの制約と完全なパラメータ説明については、[公式ドキュメント](https://developers.openai.com/api/docs/guides/image-generation)を参照してください。

## REST API

### 1. 画像生成

```bash theme={null}
curl -X POST 'https://{api_domain}/openai/v1/images/generations' \
  -H 'Authorization: Bearer apikey' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gpt-image-2.5-flare-oai",
    "prompt": "A product photo of a white ceramic mug on a wooden table",
    "size": "1024x1024",
    "quality": "high",
    "output_format": "png",
    "n": 1
  }' | jq -r '.data[0].b64_json' | base64 --decode > output.png
```

### 2. 画像編集

```bash theme={null}
curl -X POST 'https://{api_domain}/openai/v1/images/edits' \
  -H 'Authorization: Bearer apikey' \
  -F 'model=gpt-image-2' \
  -F 'image[]=@input.png' \
  -F 'mask=@mask.png' \
  -F 'prompt=Replace the background with a clean studio backdrop' \
  -F 'size=1024x1024' \
  -F 'quality=high' \
  -F 'output_format=png' \
  | jq -r '.data[0].b64_json' | base64 --decode > edited.png
```

複数画像を参照して編集する場合、`image[]` を繰り返し渡すことができます：

```bash theme={null}
curl -X POST 'https://{api_domain}/openai/v1/images/edits' \
  -H 'Authorization: Bearer apikey' \
  -F 'model=gpt-image-2' \
  -F 'image[]=@body-lotion.png' \
  -F 'image[]=@bath-bomb.png' \
  -F 'image[]=@soap.png' \
  -F 'prompt=Create a photorealistic gift basket using the reference products' \
  -F 'size=1024x1024' \
  -F 'quality=high'
```

### 3. 画像編集（JSON）

`{base64_image}` を入力PNG画像のBase64エンコードに置き換えてください。

```bash theme={null}
curl -X POST 'https://{api_domain}/openai/v1/images/edits' \
  -H 'Authorization: Bearer {your-api-key}' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gpt-image-2.5-sunburst-oai",
    "images": [
      {"image_url": "data:image/png;base64,{base64_image}"}
    ],
    "prompt": "Change the shape to green. Preserve the simple composition.",
    "size": "1024x1024",
    "quality": "xhigh",
    "moderation": "low",
    "output_format": "png"
  }'
```

## OpenAI SDK

### Python

```python theme={null}
from openai import OpenAI
import base64

api_key = "{your-api-key}"

client = OpenAI(
    api_key=api_key,
    base_url="https://{api_domain}/openai/v1",
)

result = client.images.generate(
    model="gpt-image-2.5-flare-oai",
    prompt="A product photo of a white ceramic mug on a wooden table",
    size="1024x1024",
    quality="high",
)

image_base64 = result.data[0].b64_json
with open("output.png", "wb") as f:
    f.write(base64.b64decode(image_base64))
```

画像編集：

```python theme={null}
from openai import OpenAI
import base64

api_key = "{your-api-key}"

client = OpenAI(
    api_key=api_key,
    base_url="https://{api_domain}/openai/v1",
)

result = client.images.edit(
    model="gpt-image-2",
    image=open("input.png", "rb"),
    mask=open("mask.png", "rb"),
    prompt="Replace the background with a clean studio backdrop",
    size="1024x1024",
    quality="high",
)

image_base64 = result.data[0].b64_json
with open("edited.png", "wb") as f:
    f.write(base64.b64decode(image_base64))
```

### Node.js

```javascript theme={null}
import OpenAI from "openai";
import fs from "fs";

const client = new OpenAI({
  apiKey: "{your-api-key}",
  baseURL: "https://{api_domain}/openai/v1",
});

const result = await client.images.generate({
  model: "gpt-image-2.5-flare-oai",
  prompt: "A product photo of a white ceramic mug on a wooden table",
  size: "1024x1024",
  quality: "high",
});

const imageBase64 = result.data[0].b64_json;
fs.writeFileSync("output.png", Buffer.from(imageBase64, "base64"));
```

画像編集：

```javascript theme={null}
import OpenAI, { toFile } from "openai";
import fs from "fs";

const client = new OpenAI({
  apiKey: "{your-api-key}",
  baseURL: "https://{api_domain}/openai/v1",
});

const result = await client.images.edit({
  model: "gpt-image-2",
  image: await toFile(fs.createReadStream("input.png"), null, {
    type: "image/png",
  }),
  mask: await toFile(fs.createReadStream("mask.png"), null, {
    type: "image/png",
  }),
  prompt: "Replace the background with a clean studio backdrop",
  size: "1024x1024",
  quality: "high",
});

const imageBase64 = result.data[0].b64_json;
fs.writeFileSync("edited.png", Buffer.from(imageBase64, "base64"));
```

## ベストプラクティス

### moderation パラメータ

* `moderation="auto"` はデフォルトの標準フィルタリングです。`moderation="low"` はより緩やかなフィルタリングを示します。
* `moderation` は画像生成および画像編集リクエストの両方で使用できます。
