Edit multiple images with the Imagine API
Edit multiple images with the Imagine API
POST https://api.x.ai/v1/images/edits accepts an images array of up to three sources. Order is the order you send them. Output aspect ratio follows the first image unless you set aspect_ratio. Model id is grok-imagine-image-2.0. Get a key at console.x.ai.
Each source can be a public URL, a data URI, or a Files API file_id. Mix kinds in one request.
Compose three stills
curl -X POST https://api.x.ai/v1/images/edits \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $XAI_API_KEY" \
-d '{
"model": "grok-imagine-image-2.0",
"prompt": "Show all subjects sitting together on the grass in a sunny park. Casual relaxed mood, natural daylight, warm tones. No additional people or animals.",
"images": [
{
"type": "image_url",
"url": "https://docs.x.ai/assets/api-examples/images/image-merge/woman.jpg"
},
{
"type": "image_url",
"url": "https://docs.x.ai/assets/api-examples/images/image-merge/man.jpg"
}
],
"aspect_ratio": "3:2"
}'
Python SDK uses image_urls:
import os
import xai_sdk
client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))
response = client.image.sample(
prompt="Show all subjects sitting together on the grass in a sunny park. Casual relaxed mood, natural daylight, warm tones. No additional people or animals.",
model="grok-imagine-image-2.0",
image_urls=[
"https://docs.x.ai/assets/api-examples/images/image-merge/woman.jpg",
"https://docs.x.ai/assets/api-examples/images/image-merge/man.jpg",
],
aspect_ratio="3:2",
)
print(response.url)
A single-source edit still uses the image object. This path is the images array.
Pitfalls
- Cap is three sources. A fourth is rejected.
- Image edits bill for every input still plus the generated output.
- Response URLs are temporary. Download them.
- Console API credits are a separate bill from SuperGrok's weekly pool on grok.com.