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 pairaspect_ratiowithresolution(such as2k) andquality(low,medium, orauto). Whenqualityis omitted (auto), image generation defaults tolow, whereas pinningmediumand2khelps preserve fine detail. - Batching variations: Pass
n(from1to10) on/v1/images/generationsto generate multiple variations under the sameaspect_ratio. - Output format and persistence: The default
response_formatisurl, which returns temporary hosted URLs. Requestb64_json(orbase64in 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: trueon/v1/images/generationsto receive arequest_idimmediately and pollGET /v1/images/{request_id}(which returnsurloutputs retained for 24 hours), or register webhooks forimage.generation.completedandimage.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.
Leave a Reply