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): Omittingaspect_ratio(or passingauto) 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) and2kresolutions, letting you pair exact framing with higher detail when needed. - Batching and edits: A single request can generate from
1to10images (non REST/OpenAI-compatible clients,sample()vs.sample_batch(n=...)in the xAI Python SDK), andaspect_ratioalso 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_formatdefaults tourl(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,qualityacceptslow,medium, orauto(which defaults tolowfor generation andmediumfor edits), affecting output and billing. - Long batches and async flows: Setting
deferred: truereturns arequest_idright away so you can pollGET /v1/images/{request_id}(returningurloutputs retained for 24 hours), or you can subscribe to webhook events such asimage.generation.completedandimage.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.
Leave a Reply