Control Image Aspect Ratio in xAI API with Grok Imagine

Written by

in

Controlling Image Framing with aspect_ratio in xAI’s grok-imagine-image API

When generating visual assets programmatically, getting the right framing from the start saves downstream cropping, awkward clipping, and layout rework. In xAI’s developer documentation (docs.x.ai), the /v1/images/generations endpoint paired with the grok-imagine-image model family (including grok-imagine-image-2.0) exposes an aspect_ratio parameter that lets developers specify exact output proportions or let the model choose automatically.


How aspect_ratio Works in /v1/images/generations

Requests to [https://api.x.ai/v1/images/generations](https://api.x.ai/v1/images/generations) accept a JSON payload specifying the target model, a text prompt, and optional configuration fields such as aspect_ratio.

  • Default behavior (auto): When aspect_ratio is omitted or set to "auto", the model evaluates the prompt and selects a ratio suited to the subject and composition.
  • Explicit framing: Passing a ratio string such as "16:9", "9:16", or "21:9" locks the output proportions to match your UI container, ad slot, or viewport.
  • Image editing behavior: When editing source images—including multi-image editing workflows—the output ratio defaults to matching the input image (or the first input image in a multi-image sequence) unless you explicitly override it with aspect_ratio.

Supported Aspect Ratios and Practical Use Cases

According to the xAI Image Generation documentation (docs.x.ai), aspect_ratio supports standard photographic, social, display, and ultrawide ratios alongside "auto":

Ratio Typical Use Case
1:1 Social media feeds, square cards, thumbnails
16:9 / 9:16 Widescreen video/hero frames (16:9), vertical mobile stories (9:16)
4:3 / 3:4 Presentations, structured panels (4:3), vertical portraits (3:4)
3:2 / 2:3 Classic camera and editorial photography proportions
2:1 / 1:2 Wide web banners and tall vertical slots
19.5:9 / 9:19.5 Modern tall smartphone displays (such as iPhone form factors)
20:9 / 9:20 Modern tall smartphone displays (such as Android form factors)
21:9 Cinematic widescreen compositions
5:2 Extra-wide headers and horizontal banners
auto Automatic ratio selection based on prompt context

Example API Request

Below is a cURL example calling /v1/images/generations with grok-imagine-image-2.0 and an explicit 16:9 aspect ratio:

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": "Minimalist architectural studio overlooking coastal cliffs at dawn, warm natural light",
    "aspect_ratio": "16:9"
  }'

Tips for Reliable Framing

  1. Match the ratio to the composition early: A 21:9 or 5:2 ratio favors sweeping landscapes and horizontal layouts, while 9:16, 9:19.5, or 9:20 suits full-bleed mobile screens and vertical subjects.
  2. Use auto during open exploration: When testing prompt ideas without a fixed container constraint, omitting aspect_ratio (or using "auto") lets the model pick proportions aligned with what you describe.
  3. Override explicitly during edits: If you pass a source image with one proportion into a layout that requires another, set aspect_ratio directly rather than relying on the input image’s dimensions.

Comments

Leave a Reply

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