Image API

图片生成与图片编辑

使用 UniKeyX 调用 MAI 和 GPT 图片模型,支持文生图、参考图改图、商品图和运营海报等场景。 文本和视频接口请从顶部导航进入对应单页。

Base URL
https://www.unikeyx.com/v1
生成接口
POST /images/generations
编辑接口
POST /images/edits
1

Generation

图片生成示例

MAI 生图
curl https://www.unikeyx.com/v1/images/generations \
  -H "Authorization: Bearer YOUR_UNIKEYX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "MAI-Image-2",
    "prompt": "一张高端咖啡豆礼盒的中文电商主图,白色背景,柔和棚拍光,真实产品摄影风格",
    "width": 1024,
    "height": 1024
  }'
GPT 生图
import base64
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_UNIKEYX_API_KEY",
    base_url="https://www.unikeyx.com/v1",
)

result = client.images.generate(
    model="gpt-image-2",
    prompt="A clean editorial illustration of a small AI API gateway, with modern product UI details.",
    size="1024x1024",
    quality="high",
    n=1,
)

with open("gpt-image-2-output.png", "wb") as file:
    file.write(base64.b64decode(result.data[0].b64_json))
2

Editing

参考图改图示例

MAI 改图
curl -sS https://www.unikeyx.com/v1/images/edits \
  -H "Authorization: Bearer YOUR_UNIKEYX_API_KEY" \
  -F "model=MAI-Image-2.5" \
  -F "prompt=保留商品主体,替换为夏季户外露营场景,加入中文标题:清爽上新,真实商业摄影风格" \
  -F "image=@./product-reference.png" \
| jq -r '.data[0].b64_json' \
| base64 --decode > mai-image-2-5-edit.png
GPT 改图
import base64
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_UNIKEYX_API_KEY",
    base_url="https://www.unikeyx.com/v1",
)

with open("product-reference.png", "rb") as image_file:
    result = client.images.edit(
        model="gpt-image-2",
        image=image_file,
        prompt="Keep the product shape and logo readable. Rebuild the scene as a premium e-commerce hero image.",
        size="1024x1024",
        quality="high",
    )

with open("gpt-image-2-edit.png", "wb") as file:
    file.write(base64.b64decode(result.data[0].b64_json))
3

Parameters

常用参数

model 必填。常用 MAI-Image-2MAI-Image-2.5gpt-image-2
prompt 必填。说明主体、场景、风格、构图、光线、用途和要保留/替换的内容。
width / height MAI-Image-2 使用整数宽高:宽高各不低于 768,总像素不超过 1,048,576
size GPT Image 2 使用 宽x高 字符串:16 像素步进,单边小于等于 3840,总像素 655,3608,294,400,比例限制在 1:33:1
quality GPT 图片模型可用 autolowmediumhigh。MAI 模型不建议传。
image 图片编辑必填。使用 multipart/form-data 上传 PNG 或 JPEG 等有效图片文件。

FAQ

图片生成 FAQ

图片返回 base64 而不是 URL

不同上游和通道配置可能返回不同图片字段。客户端建议同时兼容 urlb64_json

gpt-image-2 的 size 应该怎么填?

使用 宽x高 字符串,例如 1024x10241024x15361536x10242048x20483840x21602160x38401024x3072。 宽高都应能被 16 整除;单边小于等于 3840;总像素需在 655,3608,294,400 之间;宽高比限制在 1:33:1

gpt-image-2 的 quality 应该怎么填?

常用 autolowmediumhigh。 不确定时用 auto;最终出图可用 high

MAI-Image-2 的尺寸应该怎么填?

MAI-Image-2 使用 widthheight 整数字段。 宽高各不低于 768,总像素不超过 1,048,576。 常用方图 1024x1024,横图 1024x768, 竖图 768x1024。不要把 GPT 的 size 字段直接复制到 MAI 示例里。

MAI 图片模型要传 quality 吗?

不建议传 quality。想提升效果,优先优化提示词和参考图质量。

400: Unknown parameter response_format

使用 gpt-image-2 做图片编辑时,不要传 response_format。 删除旧版 DALL-E 示例中的 response_format 后重试。

400: Invalid image file or mode

先只保留一张图调用,定位是哪张文件触发错误。建议重新导出为普通 RGB PNG 或 JPEG, 并确认文件能正常打开。

生图报 The requested operation is unsupported

通常是把图片模型发到了 /chat/completions。纯生图请用 /images/generations,图片编辑请用 /images/edits