Grok Imagine Image API: Aspect Ratios in Image Generation

Written by

in

Framing Images in xAI’s Grok Imagine API with aspect_ratio

Generating images through xAI’s /v1/images/generations endpoint with grok-imagine-image-2.0 gives developers direct framing control via the aspect_ratio parameter. Rather than cropping output after generation—which risks cutting off key subjects or wasting pixels—passing aspect_ratio instructs the model to compose the scene for your exact target layout. When omitted, aspect_ratio defaults to auto, letting the model select a ratio suited to the prompt.

Supported Aspect Ratios and Layout Fit

The Grok image generation documentation maps specific aspect ratios to common screen and print formats:

Aspect Ratio Typical Use Case
1:1 Social media posts and thumbnails
16:9 / 9:16 Widescreen displays, mobile screens, and vertical stories
4:3 / 3:4 Slide presentations and portrait compositions
3:2 / 2:3 Classic photography framing
2:1 / 1:2 Web banners and page headers
19.5:9 / 9:19.5 Modern smartphone displays (iPhone)
20:9 / 9:20 Modern smartphone displays (Android)
21:9 Cinematic widescreen layouts
5:2 Extra-wide banners
auto Model selects the ratio for the prompt

Calling /v1/images/generations with aspect_ratio

You can set aspect_ratio in direct REST calls, OpenAI-compatible SDKs pointing to [https://api.x.ai/v1](https://api.x.ai/v1), or the official xAI Python SDK.

curl -X POST https://api.x.ai/v1/images/generations 
  -H "Authorization: Bearer $XAI_API_KEY" 
  -H "Content-Type: application/json" 
  -d '{
    "model": "grok-imagine-image-2.0",
    "prompt": "Wide-angle architectural view of a sunlit library atrium with tiered oak shelves",
    "aspect_ratio": "16:9",
    "resolution": "2k",
    "quality": "medium",
    "response_format": "b64_json",
    "n": 1
  }'

In Python using the OpenAI SDK against xAI’s base URL:

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["XAI_API_KEY"],
    base_url="https://api.x.ai/v1",
)

response = client.images.generate(
    model="grok-imagine-image-2.0",
    prompt="Wide-angle architectural view of a sunlit library atrium with tiered oak shelves",
    aspect_ratio="16:9",
    n=1,
)

print(response.data[0].url)

Pairing aspect_ratio with Quality, Batching, and Delivery

  • Resolution and quality: For grok-imagine-image-2.0, you can pair aspect_ratio with resolution (such as 2k) and quality (low, medium, or auto). When quality is omitted (auto), image generation defaults to low, whereas pinning medium and 2k helps preserve fine detail.
  • Batching variations: Pass n (from 1 to 10) on /v1/images/generations to generate multiple variations under the same aspect_ratio.
  • Output format and persistence: The default response_format is url, which returns temporary hosted URLs. Request b64_json (or base64 in SDKs that expose it) when you need inline data or permanent storage without relying on temporary URLs.
  • Async and deferred jobs: For large batches or long-running requests, set deferred: true on /v1/images/generations to receive a request_id immediately and poll GET /v1/images/{request_id} (which returns url outputs retained for 24 hours), or register webhooks for image.generation.completed and image.generation.failed.

Practical Framing Tips

Match your prompt’s spatial cues to the chosen aspect_ratio. A 21:9 or 16:9 ratio works best when you describe horizontal sweep, environmental context, or wide camera angles, while 9:16, 9:19.5, or 3:4 suits vertical subjects and mobile viewports. State clearly what stays in frame and what the edges show so the model uses the extra width or height intentionally rather than padding empty space.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *