API 参考OpenAI Images
OpenAI 图片生成概览
OpenAI 图片生成
与 OpenAI 官方 Images API 完全兼容,可直接使用 openai Python/Node.js SDK,仅需替换
base_url和api_key。
平台集成了 OpenAI 的 GPT-Image-2 模型,提供文本生成图片和图片编辑两种操作。
功能特性
- 文本生成图片:通过文本提示词创建图像,支持任意尺寸(需符合约束条件)
- 图片编辑:基于原始图像和提示词进行编辑,支持多图合成、遮罩编辑
- 多种输出格式:支持 PNG、JPEG、WebP 格式,JPEG/WebP 可设置压缩率
- 质量控制:支持 low、medium、high 三种质量等级
如何使用 OpenAI 图片
文本生成图片
调用 /v1/images/generations 端点,传入提示词和参数:
from openai import OpenAI
import base64
client = OpenAI(
api_key="your-api-key",
base_url="https://api.3hei.com"
)
result = client.images.generate(
model="gpt-image-2",
prompt="一本儿童绘本插画,画着兽医正用听诊器倾听一只小水獭的心跳。"
)
image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)
with open("otter.png", "wb") as f:
f.write(image_bytes)import OpenAI from 'openai';
import fs from 'fs';
const client = new OpenAI({
apiKey: 'your-api-key',
baseURL: 'https://api.3hei.com'
});
const result = await client.images.generate({
model: 'gpt-image-2',
prompt: '一本儿童绘本插画,画着兽医正用听诊器倾听一只小水獭的心跳。'
});
const image_bytes = Buffer.from(result.data[0].b64_json, 'base64');
fs.writeFileSync('otter.png', image_bytes);curl -X POST "https://api.3hei.com/v1/images/generations" \
-H "Authorization: Bearer your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "一本儿童绘本插画,画着兽医正用听诊器倾听一只小水獭的心跳。"
}' | jq -r '.data[0].b64_json' | base64 --decode > otter.png图片编辑
调用 /v1/images/edits 端点,传入参考图片和提示词,支持多图合成:
import base64
from openai import OpenAI
client = OpenAI(
api_key="your-api-key",
base_url="https://api.3hei.com"
)
prompt = """
生成一张逼真的礼品篮照片,白色背景,带有'Relax & Unwind'标签和手写字体风格的丝带,
包含所有参考图片中的物品。
"""
result = client.images.edit(
model="gpt-image-2",
image=[
open("body-lotion.png", "rb"),
open("bath-bomb.png", "rb"),
open("incense-kit.png", "rb"),
open("soap.png", "rb"),
],
prompt=prompt
)
image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)
with open("gift-basket.png", "wb") as f:
f.write(image_bytes)import fs from 'fs';
import OpenAI, { toFile } from 'openai';
const client = new OpenAI({
apiKey: 'your-api-key',
baseURL: 'https://api.3hei.com'
});
const prompt = `
生成一张逼真的礼品篮照片,白色背景,带有'Relax & Unwind'标签和手写字体风格的丝带,
包含所有参考图片中的物品。
`;
const imageFiles = [
'bath-bomb.png',
'body-lotion.png',
'incense-kit.png',
'soap.png',
];
const images = await Promise.all(
imageFiles.map(async (file) =>
await toFile(fs.createReadStream(file), null, { type: 'image/png' })
)
);
const result = await client.images.edit({
model: 'gpt-image-2',
image: images,
prompt,
});
const image_bytes = Buffer.from(result.data[0].b64_json, 'base64');
fs.writeFileSync('gift-basket.png', image_bytes);curl -X POST "https://api.3hei.com/v1/images/edits" \
-H "Authorization: Bearer your-api-key" \
-F "model=gpt-image-2" \
-F "image[]=@body-lotion.png" \
-F "image[]=@bath-bomb.png" \
-F "image[]=@incense-kit.png" \
-F "image[]=@soap.png" \
-F 'prompt=生成一张逼真的礼品篮照片,白色背景,带有Relax & Unwind标签和手写字体风格的丝带,包含所有参考图片中的物品' \
-o >(jq -r '.data[0].b64_json' | base64 --decode > gift-basket.png)基本信息
Base URL: https://api.3hei.com
认证方式
与 OpenAI 官方一致:使用 Authorization: Bearer <your-api-key> Header。
支持模型
加载模型列表中...
关键参数
尺寸(size)
GPT-Image-2 支持任意分辨率,但需符合以下约束:
- 最大边长 ≤ 3840px
- 两边必须是 16px 的倍数
- 长边与短边比例 ≤ 3:1
- 总像素 ≥ 655,360 且 ≤ 8,294,400
常用尺寸:
| 尺寸 | 说明 |
|---|---|
1024x1024 | 正方形(默认) |
1536x1024 | 横向 |
1024x1536 | 纵向 |
2048x2048 | 2K 正方形 |
2048x1152 | 2K 横向 |
3840x2160 | 4K 横向 |
2160x3840 | 4K 纵向 |
auto | 自动选择 |
质量(quality)
| 值 | 说明 |
|---|---|
low | 快速生成,适合预览 |
medium | 中等质量 |
high | 高质量输出 |
auto | 自动选择(默认) |
输出格式(output_format)
| 值 | 说明 |
|---|---|
png | PNG 格式(默认) |
jpeg | JPEG 格式,速度快 |
webp | WebP 格式 |
使用 jpeg 或 webp 时,可通过 output_compression(0-100)设置压缩率。
内容审核(moderation)
| 值 | 说明 |
|---|---|
auto | 标准过滤(默认) |
low | 较宽松的过滤 |
限制
- 延迟:复杂提示词可能需要最多 2 分钟
- 文本渲染:虽然已大幅改进,但精确文本放置仍有挑战
- 透明背景:GPT-Image-2 不支持透明背景