Skip to content

Grok Imagine Image

Grok Imagine Image 使用 OpenAI 兼容的 Images 接口调用,适合文生图、海报、产品图和创意视觉生成。

接口地址

http
POST /v1/images/generations
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json

图片编辑 / 图生图使用:

http
POST /v1/images/edits
Authorization: Bearer YOUR_API_KEY
Content-Type: multipart/form-data

支持模型

模型说明
grok-imagine-image标准图片生成模型,适合大多数文生图场景
grok-imagine-image-pro高质量图片生成模型,适合更重视画面细节、质感和稳定性的场景
grok-imagine-image-edit图片编辑 / 图生图模型,适合基于参考图修改、重绘或风格转换

实际可用模型以账户权限和平台配置为准。普通出图优先使用 grok-imagine-image;需要更高质量时再使用 grok-imagine-image-pro;带参考图编辑时推荐使用 grok-imagine-image-edit

请求参数

参数类型必填说明
modelstringgrok-imagine-imagegrok-imagine-image-pro
promptstring图片生成描述
ninteger生成图片数量,默认 1,范围 110
sizestring输出尺寸,默认 1024x1024
response_formatstring返回格式,支持 urlb64_json,默认 url

图片编辑参数

/v1/images/edits 使用 multipart/form-data,至少上传一张参考图。

参数类型必填说明
modelstring推荐使用 grok-imagine-image-edit
imagefile参考图文件字段。多图时可重复传 image 字段
promptstring图片编辑描述
ninteger生成图片数量,默认 1
sizestring输出尺寸,默认 1024x1024
response_formatstring返回格式,支持 urlb64_json,默认 url

为了兼容客户端选择模型的习惯,编辑接口也接受 grok-imagine-imagegrok-imagine-image-progrok-imagine-image-lite 作为 model,但建议显式传 grok-imagine-image-edit

支持尺寸

size 使用 WIDTHxHEIGHT 格式。常用取值:

size比例
1024x10241:1
1280x72016:9
720x12809:16
1792x10243:2
1024x17922:3

如果不确定比例,建议先使用 1024x1024720x1280

请求示例

bash
curl -X POST https://cubicspaces.cloud/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "grok-imagine-image",
    "prompt": "A clean product photo of a transparent glass spaceship on a white studio background, soft lighting, high detail",
    "size": "1024x1024",
    "n": 1,
    "response_format": "url"
  }'

高质量模型示例:

bash
curl -X POST https://cubicspaces.cloud/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "grok-imagine-image-pro",
    "prompt": "A premium watch product advertisement, black marble surface, dramatic rim light, realistic reflections, luxury editorial style",
    "size": "1792x1024",
    "n": 1,
    "response_format": "b64_json"
  }'

图片编辑 / 图生图示例:

bash
curl -X POST https://cubicspaces.cloud/v1/images/edits \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -F "model=grok-imagine-image-edit" \
  -F "image=@reference.png" \
  -F "prompt=Keep the main subject, change the background to a bright modern studio, realistic lighting" \
  -F "size=1024x1024" \
  -F "response_format=url"

返回格式

response_formaturl 时:

json
{
  "created": 1770000000,
  "data": [
    {
      "url": "https://example.com/generated-image.png",
      "revised_prompt": "A clean product photo..."
    }
  ]
}

response_formatb64_json 时:

json
{
  "created": 1770000000,
  "data": [
    {
      "b64_json": "iVBORw0KGgo...",
      "revised_prompt": "A premium watch product advertisement..."
    }
  ]
}

客户端应从 data[].urldata[].b64_json 读取图片结果。

SDK 示例

python
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://cubicspaces.cloud/v1",
)

result = client.images.generate(
    model="grok-imagine-image",
    prompt="A clean product photo of a tiny glass spaceship on a white background",
    size="1024x1024",
    n=1,
    response_format="url",
)

print(result.data[0].url)
python
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://cubicspaces.cloud/v1",
)

result = client.images.edit(
    model="grok-imagine-image-edit",
    image=open("reference.png", "rb"),
    prompt="Keep the main subject, change the background to a bright modern studio",
    size="1024x1024",
    response_format="url",
)

print(result.data[0].url)
js
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: "YOUR_API_KEY",
  baseURL: "https://cubicspaces.cloud/v1"
});

const result = await client.images.generate({
  model: "grok-imagine-image",
  prompt: "A clean product photo of a tiny glass spaceship on a white background",
  size: "1024x1024",
  n: 1,
  response_format: "url"
});

console.log(result.data[0].url);
js
import fs from "node:fs";
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: "YOUR_API_KEY",
  baseURL: "https://cubicspaces.cloud/v1"
});

const result = await client.images.edit({
  model: "grok-imagine-image-edit",
  image: fs.createReadStream("reference.png"),
  prompt: "Keep the main subject, change the background to a bright modern studio",
  size: "1024x1024",
  response_format: "url"
});

console.log(result.data[0].url);

注意事项

  • grok-imagine-image 推荐作为默认图片模型。
  • grok-imagine-image-pro 更适合产品图、广告图、细节要求高的图片。
  • grok-imagine-image-edit 用于带参考图的图片编辑,请调用 /v1/images/edits 并上传 image 文件字段。
  • 图片生成是非流式接口,建议客户端等待完整响应后读取结果。
  • 如返回内容审核或上游失败错误,请调整提示词后重试。
  • 实际可用模型和并发能力以账户权限和平台配置为准。