图片生成-OpenAI Image 协议
ZenMux 支持通过 OpenAI Image 协议调用 OpenAI 系列图片生成模型。你可以继续使用 OpenAI SDK,只需要把 Base URL 指向 ZenMux,并使用 ZenMux API Key。
支持模型
ZenMux 支持的 OpenAI 图片生成模型会持续更新。请前往 ZenMux 模型目录 查询当前可用模型。
适用场景
OpenAI Image 协议适合以下场景:
- 根据文本提示词生成图片。
- 基于一张或多张参考图生成新图片。
- 使用 mask 对图片局部区域进行编辑。
- 通过流式事件获取 partial image,做更实时的生成体验。
如果你需要使用 Google Gemini 协议调用任意图片生成模型,请参考 图片生成。OpenAI 系列图片模型同时支持 OpenAI Image 协议和 Google Gemini 协议。
接入方式
OpenAI Image 协议使用 OpenAI SDK 初始化方式:
TypeScript
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://zenmux.ai/api/v1",
apiKey: process.env.ZENMUX_API_KEY,
});Python
from openai import OpenAI
client = OpenAI(
base_url="https://zenmux.ai/api/v1",
api_key="<ZENMUX_API_KEY>",
)bash
curl https://zenmux.ai/api/v1/images/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $ZENMUX_API_KEY"生成图片
使用 images.generate 根据文本提示词生成图片。GPT Image 模型默认返回 b64_json,你可以将其解码后保存为图片文件。
TypeScript
import OpenAI from "openai";
import { writeFile } from "fs/promises";
const client = new OpenAI({
baseURL: "https://zenmux.ai/api/v1",
apiKey: process.env.ZENMUX_API_KEY,
});
const img = await client.images.generate({
model: "gpt-image-2",
prompt: "A cute baby sea otter",
n: 1,
size: "1024x1024",
});
const imageBuffer = Buffer.from(img.data[0].b64_json, "base64");
await writeFile("output.png", imageBuffer);Python
import base64
from openai import OpenAI
client = OpenAI(
base_url="https://zenmux.ai/api/v1",
api_key="<ZENMUX_API_KEY>",
)
img = client.images.generate(
model="gpt-image-2",
prompt="A cute baby sea otter",
n=1,
size="1024x1024",
)
image_bytes = base64.b64decode(img.data[0].b64_json)
with open("output.png", "wb") as f:
f.write(image_bytes)bash
curl https://zenmux.ai/api/v1/images/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $ZENMUX_API_KEY" \
-d '{
"model": "gpt-image-2",
"prompt": "A cute baby sea otter",
"n": 1,
"size": "1024x1024"
}' | jq -r '.data[0].b64_json' | base64 --decode > output.png编辑图片
使用 images.edit 可以基于一张或多张参考图片生成新图片,也可以配合 mask 做局部编辑。
使用参考图生成新图片
TypeScript
import fs from "fs";
import OpenAI, { toFile } from "openai";
const client = new OpenAI({
baseURL: "https://zenmux.ai/api/v1",
apiKey: process.env.ZENMUX_API_KEY,
});
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 rsp = await client.images.edit({
model: "gpt-image-2",
image: images,
prompt: "Create a lovely gift basket with these four items in it",
});
// Save the image to a file
const image_base64 = rsp.data[0].b64_json;
const image_bytes = Buffer.from(image_base64, "base64");
fs.writeFileSync("basket.png", image_bytes);Python
import base64
import os
from openai import OpenAI
client = OpenAI(
base_url="https://zenmux.ai/api/v1",
api_key=os.environ["ZENMUX_API_KEY"],
)
prompt = """
Generate a photorealistic image of a gift basket on a white background
labeled 'Relax & Unwind' with a ribbon and handwriting-like font,
containing all the items in the reference pictures.
"""
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)
# Save the image to a file
with open("gift-basket.png", "wb") as f:
f.write(image_bytes)bash
curl -s -D >(grep -i x-request-id >&2) \
-o >(jq -r '.data[0].b64_json' | base64 --decode > gift-basket.png) \
-X POST "https://zenmux.ai/api/v1/images/edits" \
-H "Authorization: Bearer $ZENMUX_API_KEY" \
-F "model=openai/gpt-image-2" \
-F "image[][email protected]" \
-F "image[][email protected]" \
-F "image[][email protected]" \
-F "image[][email protected]" \
-F 'prompt=Create a lovely gift basket with these four items in it'使用 mask 局部编辑
mask 用来告诉模型图片中需要编辑的区域。输入图片和 mask 应保持相同格式与尺寸;使用 PNG mask 时,建议包含 alpha channel。
TypeScript
import fs from "fs";
import OpenAI, { toFile } from "openai";
const client = new OpenAI({
baseURL: "https://zenmux.ai/api/v1",
apiKey: process.env.ZENMUX_API_KEY,
});
const rsp = await client.images.edit({
model: "gpt-image-2",
image: await toFile(fs.createReadStream("sunlit-lounge.png"), null, {
type: "image/png",
}),
mask: await toFile(fs.createReadStream("mask.png"), null, {
type: "image/png",
}),
prompt: "A sunlit indoor lounge area with a pool containing a flamingo",
});
const imageBase64 = rsp.data[0].b64_json;
const imageBytes = Buffer.from(imageBase64, "base64");
fs.writeFileSync("lounge.png", imageBytes);Python
import base64
from openai import OpenAI
client = OpenAI(
base_url="https://zenmux.ai/api/v1",
api_key="<ZENMUX_API_KEY>",
)
result = client.images.edit(
model="gpt-image-2",
image=open("sunlit-lounge.png", "rb"),
mask=open("mask.png", "rb"),
prompt="A sunlit indoor lounge area with a pool containing a flamingo",
)
image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)
with open("lounge.png", "wb") as f:
f.write(image_bytes)bash
curl -s -D >(grep -i x-request-id >&2) \
-o >(jq -r '.data[0].b64_json' | base64 --decode > lounge.png) \
-X POST "https://zenmux.ai/api/v1/images/edits" \
-H "Authorization: Bearer $ZENMUX_API_KEY" \
-F "model=openai/gpt-image-2" \
-F "[email protected]" \
-F "image[][email protected]" \
-F 'prompt=A sunlit indoor lounge area with a pool containing a flamingo'流式生成
OpenAI Image 协议支持通过 stream: true 返回 SSE 事件。你可以使用 partial_images 控制中间图数量:
partial_images: 0:只返回最终图片。partial_images: 1到3:返回部分生成图,最终图可能提前完成。
TypeScript
import fs from "fs";
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://zenmux.ai/api/v1",
apiKey: process.env.ZENMUX_API_KEY,
});
const stream = await client.images.generate({
model: "gpt-image-2",
prompt: "Draw a river made of white owl feathers through a winter landscape",
stream: true,
partial_images: 2,
});
for await (const event of stream) {
if (event.type === "image_generation.partial_image") {
const imageBuffer = Buffer.from(event.b64_json, "base64");
fs.writeFileSync(`river-${event.partial_image_index}.png`, imageBuffer);
}
}Python
import base64
from openai import OpenAI
client = OpenAI(
base_url="https://zenmux.ai/api/v1",
api_key="<ZENMUX_API_KEY>",
)
stream = client.images.generate(
model="gpt-image-2",
prompt="Draw a river made of white owl feathers through a winter landscape",
stream=True,
partial_images=2,
)
for event in stream:
if event.type == "image_generation.partial_image":
image_bytes = base64.b64decode(event.b64_json)
with open(f"river-{event.partial_image_index}.png", "wb") as f:
f.write(image_bytes)bash
curl https://zenmux.ai/api/v1/images/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $ZENMUX_API_KEY" \
-d '{
"model": "gpt-image-2",
"prompt": "Draw a river made of white owl feathers through a winter landscape",
"stream": true,
"partial_images": 2
}' \
--no-buffer流式事件格式可参考:
输出参数
常用输出参数包括:
| 参数 | 说明 |
|---|---|
size | 图片尺寸,例如 1024x1024、1536x1024、1024x1536 或模型支持的其他尺寸 |
quality | 输出质量,例如 low、medium、high、auto |
output_format | 输出格式,例如 png、jpeg、webp |
output_compression | JPEG / WebP 压缩质量,取值 0-100 |
background | 背景设置,例如 transparent、opaque、auto,具体支持情况取决于模型 |
完整接口字段请参考: