Grok Imagine Image API: Aspect Ratio & Generation Guide

Written by

in

Framing the Shot: How aspect_ratio Shapes Image Generation in xAI’s Grok Imagine

A great prompt can still fall flat if the canvas is the wrong shape. A wide mountain panorama squeezed into a vertical slot loses breath; a tall portrait jammed into a banner crops out the head. In xAI’s Grok image documentation (docs.x.ai), the aspect_ratio parameter on image generation and editing endpoints lets you lock down the frame before pixels are drawn—or hand that choice to the model with auto.


How aspect_ratio Works Across Grok Image Endpoints

On xAI’s image APIs (including /v1/images/generations and editing workflows with grok-imagine-image and grok-imagine-image-2.0), aspect_ratio defines the proportion between width and height. You pair it with resolution (1k by default, or 2k for sharper detail) and, on grok-imagine-image-2.0, quality (low, medium, or auto).

When you omit aspect_ratio, xAI’s documentation notes that it defaults to auto, letting the model select a ratio suited to your prompt. On image edits—such as multi-image edits—the output defaults to matching the first input image unless you explicitly pass aspect_ratio to override it.

Supported Ratios and Practical Use Cases

The xAI image generation documentation groups aspect_ratio values around real display, editorial, and creative targets:

Ratio(s) Typical Use Cases
1:1 Social media posts, square thumbnails, balanced cards
16:9 / 9:16 Widescreen video/headers vs. vertical mobile and stories
4:3 / 3:4 Presentations, slide visuals, standard portraits
3:2 / 2:3 Classic photography framing and editorial layouts
2:1 / 1:2 Wide banners, split headers, tall vertical strips
19.5:9 / 9:19.5 Modern smartphone displays such as iPhone proportions
20:9 / 9:20 Tall modern smartphone displays such as Android proportions
21:9 Cinematic widescreen scenes
5:2 Extra-wide web or hero banners
auto Model chooses the best ratio for the prompt

Code Patterns: REST, OpenAI SDK, and xAI SDK

Whether you call the REST API directly, use an OpenAI-compatible client pointed at [https://api.x.ai/v1](https://api.x.ai/v1), or use xAI’s SDK, passing aspect_ratio keeps layout geometry deterministic across runs.

Direct REST (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": "A mountain landscape at sunrise with warm light on distant ridges",
    "aspect_ratio": "16:9",
    "resolution": "2k"
  }'

Python (xai_sdk and OpenAI-Compatible Client)

import os
from openai import OpenAI
import xai_sdk

# Option A: xAI Python SDK
client = xai_sdk.Client()
response = client.image.sample(
    prompt="A mountain landscape at sunrise",
    model="grok-imagine-image-2.0",
    aspect_ratio="16:9",
)
print(response.url)

# Option B: OpenAI-compatible SDK against xAI's base URL
openai_client = OpenAI(
    api_key=os.environ["XAI_API_KEY"],
    base_url="https://api.x.ai/v1",
)
gen = openai_client.images.generate(
    model="grok-imagine-image-2.0",
    prompt="Editorial studio portrait of a ceramicist at a wheel",
    aspect_ratio="3:4",
)
print(gen.data[0].url)

Practical Tips for Better Framing

  • Lock aspect_ratio in production UIs: Use auto while brainstorming or exploring prompts, but specify an exact ratio like 16:9, 3:4, or 19.5:9 when your UI container has fixed dimensions so post-generation cropping does not clip key subjects.
  • Match subject and orientation: Pair sweeping horizons, streetscapes, and architectural wide shots with horizontal ratios (16:9, 21:9, 3:2), and pair standing subjects or mobile screens with vertical ratios (3:4, 9:16, 9:19.5, 9:20).
  • Pair with resolution and storage choices: Set resolution to 2k when fine details matter, and remember that returned URLs are temporary—request base64 or save assets to durable storage when building pipelines.

Comments

Leave a Reply

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