Grok Imagine Image API: Aspect Ratio & Generations Guide

Written by

in

Framing Every Shot: How aspect_ratio Shapes Image Generation in Grok Imagine (docs.x.ai)

An aspect ratio is not an afterthought in visual design—it decides what enters the frame, what gets cropped out, and how a viewer’s eye travels across the scene. In xAI’s documentation (docs.x.ai), image generation centers on the grok-imagine-image family (including grok-imagine-image-2.0) and the /v1/images/generations endpoint, giving developers direct control over framing alongside resolution, batch size, quality, and asynchronous execution.


How aspect_ratio Works on /v1/images/generations

When calling /v1/images/generations—whether directly via REST, through an OpenAI-compatible client pointing at [https://api.x.ai/v1](https://api.x.ai/v1), or via xAI’s SDKs—the aspect_ratio parameter sets the target framing of the generated image.

  • Automatic selection (auto): Omitting aspect_ratio (or passing auto) lets the model choose a ratio suited to your prompt. That is convenient for quick exploration, but explicit ratios prevent unwanted cropping in production UIs.
  • Resolution pairing: Grok image generation supports 1k (default when omitted) and 2k resolutions, letting you pair exact framing with higher detail when needed.
  • Batching and edits: A single request can generate from 1 to 10 images (n on REST/OpenAI-compatible clients, sample() vs. sample_batch(n=...) in the xAI Python SDK), and aspect_ratio also applies on image editing workflows (/v1/images/edits).

Supported Aspect Ratios and Where They Fit

xAI’s image generation docs group supported ratios around screen shapes, photography traditions, and widescreen layouts:

Aspect Ratio(s) Typical Use Cases
1:1 Social media feeds, square cards, thumbnails
16:9 / 9:16 Widescreen banners, video thumbnails, mobile screens, vertical stories
4:3 / 3:4 Slide decks, UI panels, editorial portraits
3:2 / 2:3 Classic photography framing, landscape and vertical prints
19.5:9 / 9:19.5 Modern tall smartphone displays (such as iPhone screens)
20:9 / 9:20 Modern tall smartphone displays (such as Android screens)
21:9 / 5:2 Ultra-wide cinematic shots, wide headers, panoramic banners

Choosing the ratio before writing the prompt changes how you describe composition. A 9:16 or 9:20 vertical frame favors taller subjects, leading lines from foreground to background, and breathing room for top/bottom UI overlays, whereas 16:9 or 21:9 rewards sweeping horizons, environmental context, and horizontal staging.


Practical Request Patterns

OpenAI-Compatible Python Client

import OpenAI

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

response = client.images.generate(
    model="grok-imagine-image-2.0",
    prompt="Mountain landscape at sunrise, crisp ridge lines and warm morning light",
    aspect_ratio="16:9",
)

print(response.data[0].url)

Direct REST Call (curl)

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": "Editorial still life on a pale oak table with soft daylight",
    "aspect_ratio": "3:2",
    "resolution": "2k",
    "quality": "medium",
    "response_format": "b64_json",
    "n": 1
  }'

A few operational details from the docs.x.ai image generation guide are worth keeping in mind as you scale beyond single calls:

  • Output format: response_format defaults to url (temporary hosted URLs) and also supports base64 embedding when you want data directly inline without a separate download step.
  • Quality controls: On grok-imagine-image-2.0, quality accepts low, medium, or auto (which defaults to low for generation and medium for edits), affecting output and billing.
  • Long batches and async flows: Setting deferred: true returns a request_id right away so you can poll GET /v1/images/{request_id} (returning url outputs retained for 24 hours), or you can subscribe to webhook events such as image.generation.completed and image.generation.failed.

Prompting Tips That Match the Frame

xAI’s image prompting guide emphasizes locking down the subject first—what it is, its form, count, materials, and lighting—before layering stylistic details or camera crop. Pairing a deliberate aspect_ratio with explicit composition cues (what stays in frame, what gets cropped, and where the focal point sits) keeps generated assets consistent across multi-image sets and UI slots.

Comments

Leave a Reply

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