> For the complete documentation index, see [llms.txt](https://docs.luojixiangliang.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.luojixiangliang.com/reference/openai-api-can-kao-wen-dang/tu-pian.md).

# 图片

给定提示和/或输入图片，模型将生成一个新图片。

### 生成图片

```
POST https://api.openai.com/v1/images/generations
```

给定提示创建图片。

#### 请求示例

{% tabs %}
{% tab title="curl" %}
{% code lineNumbers="true" %}

```sh
curl https://api.openai.com/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "prompt": "A cute baby sea otter",
    "n": 2,
    "size": "1024x1024"
  }'
```

{% endcode %}
{% endtab %}

{% tab title="python" %}
{% code lineNumbers="true" %}

```python
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.Edit.create(
  model="text-davinci-edit-001",
  input="What day of the wek is it?",
  instruction="Fix the spelling mistakes"
)

```

{% endcode %}
{% endtab %}

{% tab title="node.js" %}
{% code lineNumbers="true" %}

```javascript
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createEdit({
  model: "text-davinci-edit-001",
  input: "What day of the wek is it?",
  instruction: "Fix the spelling mistakes",
});

```

{% endcode %}
{% endtab %}
{% endtabs %}

{% code title="请求参数" lineNumbers="true" %}

```json
{
  "prompt": "A cute baby sea otter",
  "n": 2,
  "size": "1024x1024"
}
```

{% endcode %}

{% code title="返回结果" lineNumbers="true" %}

```json
{
  "created": 1589478378,
  "data": [
    {
      "url": "https://..."
    },
    {
      "url": "https://..."
    }
  ]
}
```

{% endcode %}

#### 请求主体

* prompt：字符串 必需 所需图像的文本描述。最大长度为1000个字符。
* n：整数 可选的 默认为1 要生成的图像数量。必须介于1和10之间。
* size：字符串 可选的 默认为1024x1024 生成的图像大小。必须是256x256、512x512或1024x1024之一。
* response\_format： 字符串 可选的 默认为url 返回生成图像的格式。必须是url或b64\_json之一。
* user： 字符串 可选的 代表您的终端用户的唯一标识符，可帮助OpenAI监视和检测滥用。了解更多。

### 图片编辑

```
POST https://api.openai.com/v1/images/edits
```

给定原始图像和提示，创建编辑或扩展的图像。

#### 请求主体

* image： 字符串 必需 要编辑的图像。必须是有效的PNG文件，小于4MB且为正方形。如果未提供mask，则图像必须具有透明度，该透明度将用作mask。
* mask： 字符串 可选的 另一个图像，其中完全透明的区域（例如alpha为零的区域）表明应该在哪里编辑图片。必须是有效的PNG文件，大小不超过4MB，且尺寸与图片相同。
* prompt： 字符串 必需 最多1000个字符的图像描述文本。
* n：整数 可选的 默认为1 要生成的图像数量。必须在1和10之间。
* size：字符串 可选的 默认为1024x1024 生成的图像大小。必须为256x256、512x512或1024x1024之一。
* response\_format：字符串 可选的 默认为url 生成的图像返回的格式。必须是url或b64\_json之一。
* user：字符串 可选的 表示您的最终用户的唯一标识符，可以帮助OpenAI监视和检测滥用。了解更多信息。

#### 请求示例

{% tabs %}
{% tab title="curl" %}
{% code lineNumbers="true" %}

```sh
curl https://api.openai.com/v1/images/edits \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -F image="@otter.png" \
  -F mask="@mask.png" \
  -F prompt="A cute baby sea otter wearing a beret" \
  -F n=2 \
  -F size="1024x1024"

```

{% endcode %}
{% endtab %}

{% tab title="python" %}
{% code lineNumbers="true" %}

```python
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.Image.create_edit(
  image=open("otter.png", "rb"),
  mask=open("mask.png", "rb"),
  prompt="A cute baby sea otter wearing a beret",
  n=2,
  size="1024x1024"
)

```

{% endcode %}
{% endtab %}

{% tab title="node.js" %}
{% code lineNumbers="true" %}

```javascript
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createImageEdit(
  fs.createReadStream("otter.png"),
  fs.createReadStream("mask.png"),
  "A cute baby sea otter wearing a beret",
  2,
  "1024x1024"
);

```

{% endcode %}
{% endtab %}
{% endtabs %}

{% code title="返回结果" lineNumbers="true" %}

```json
{
  "created": 1589478378,
  "data": [
    {
      "url": "https://..."
    },
    {
      "url": "https://..."
    }
  ]
}

```

{% endcode %}

### 创建图片变体

```
POST https://api.openai.com/v1/images/variations
```

生成一个给定图像的变体。

#### 请求体

* image 字符串 必需的 作为变化基础的图像。必须是有效的PNG文件，小于4MB，且为正方形。
* n 整数 可选的 默认为1 生成图像的数量。必须介于1和10之间。
* size 字符串 可选的 默认为1024x1024 生成图像的尺寸。必须是256x256、512x512或1024x1024之一。
* response\_format 字符串 可选的 默认为url 生成的图像返回的格式。必须是url或b64\_json之一。
* user 字符串 可选的 表示您的最终用户的唯一标识符，可帮助OpenAI监测和检测滥用。了解更多。

#### 请求示例

{% tabs %}
{% tab title="curl" %}
{% code lineNumbers="true" %}

```sh
curl https://api.openai.com/v1/images/variations \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -F image="@otter.png" \
  -F n=2 \
  -F size="1024x1024"

```

{% endcode %}
{% endtab %}

{% tab title="python" %}
{% code lineNumbers="true" %}

```python
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.Image.create_variation(
  image=open("otter.png", "rb"),
  n=2,
  size="1024x1024"
)

```

{% endcode %}
{% endtab %}

{% tab title="node.js" %}
{% code lineNumbers="true" %}

```javascript
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createImageVariation(
  fs.createReadStream("otter.png"),
  2,
  "1024x1024"
);

```

{% endcode %}
{% endtab %}
{% endtabs %}

{% code title="返回结果" lineNumbers="true" %}

```json
{
  "created": 1589478378,
  "data": [
    {
      "url": "https://..."
    },
    {
      "url": "https://..."
    }
  ]
}

```

{% endcode %}
