> ## Documentation Index
> Fetch the complete documentation index at: https://docs.modular.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Image generation

> Generate and transform images with the responses API

Generate images from text prompts or transform existing images with the
[responses API](/api/inference/create-response). The API returns
generated images as base64-encoded data.

For information about all request and response fields, see the
[responses API reference](/api/inference/create-response). For
available image generation models, see [Supported models](/models).

<Tip>
  The responses API follows the provider-agnostic
  [Open Responses](https://huggingface.co/blog/open-responses) specification.
</Tip>

## Generate an image

To generate an image, set `input` to a text description of the image and
set generation parameters in `provider_options.image`.

The following example generates an image from a text prompt and saves it as
`output-text-to-image.png`.

<Tabs>
  <Tab title="Python">
    Send a prompt, decode the returned image data, and save the image:

    ```python title="generate-image.py" theme={null}
    import base64
    from pathlib import Path

    from openai import OpenAI

    client = OpenAI(
        base_url="https://api.modular.com/v1",
        api_key="<your-api-key>",  # Load your key
    )

    response = client.responses.create(
        model="black-forest-labs/FLUX.2-dev",
        input="A serene mountain landscape at sunset",
        extra_body={
            "provider_options": {
                "image": {
                    "height": 512,
                    "width": 512,
                    "steps": 28,
                }
            }
        },
    )

    image_data = response.output[0].content[0].image_data
    Path("output-text-to-image.png").write_bytes(base64.b64decode(image_data))
    ```
  </Tab>

  <Tab title="curl">
    This example runs on Linux and requires `jq`. Send a prompt and decode the
    returned image data:

    ```bash theme={null}
    curl -X POST https://api.modular.com/v1/responses \
      -H "Authorization: Bearer <your-api-key>" \
      -H "Content-Type: application/json" \
      -d '{
        "model": "black-forest-labs/FLUX.2-dev",
        "input": "A serene mountain landscape at sunset",
        "provider_options": {
          "image": {
            "height": 512,
            "width": 512,
            "steps": 28
          }
        }
      }' \
      | jq -r '.output[0].content[0].image_data' \
      | base64 -d > output-text-to-image.png
    ```
  </Tab>
</Tabs>

The generated image should look similar to the following:

<img src="https://mintcdn.com/modular/21dnEKVIFHhCZ_2p/inference/images/output-text-to-image.png?fit=max&auto=format&n=21dnEKVIFHhCZ_2p&q=85&s=f5e8b9437024850eeb3e9b17df8c51dc" alt="Text-to-image output: a serene mountain landscape at sunset." width="1024" height="1024" data-path="inference/images/output-text-to-image.png" />

## Transform an image

To transform an image, set `input` to a user message containing:

* An `input_image` block with an image URL or base64-encoded data URI.
* An `input_text` block describing the transformation.

The following example reads a local image named `input.png`, encodes it as a
data URI, and saves the transformed image.

<Tabs>
  <Tab title="Python">
    Encode the input image, send it with a prompt, and save the transformed image:

    ```python title="transform-image.py" theme={null}
    import base64
    from pathlib import Path

    from openai import OpenAI

    client = OpenAI(
        base_url="https://api.modular.com/v1",
        api_key="<your-api-key>",  # Load your key
    )

    input_data = base64.b64encode(Path("input.png").read_bytes()).decode("utf-8")

    response = client.responses.create(
        model="black-forest-labs/FLUX.2-dev",
        input=[
            {
                "role": "user",
                "content": [
                    {
                        "type": "input_image",
                        "image_url": f"data:image/png;base64,{input_data}",
                    },
                    {
                        "type": "input_text",
                        "text": "Transform this image into a watercolor painting.",
                    },
                ],
            }
        ],
        extra_body={
            "provider_options": {
                "image": {
                    "height": 512,
                    "width": 512,
                    "steps": 28,
                }
            }
        },
    )

    image_data = response.output[0].content[0].image_data
    Path("output-image-to-image.png").write_bytes(base64.b64decode(image_data))
    ```
  </Tab>

  <Tab title="curl">
    This example runs on Linux and requires `jq`. Encode the input image and write
    the request body to a file:

    ```bash theme={null}
    IMAGE_DATA=$(base64 -w 0 input.png)

    cat > request.json <<EOF
    {
      "model": "black-forest-labs/FLUX.2-dev",
      "input": [
        {
          "role": "user",
          "content": [
            {
              "type": "input_image",
              "image_url": "data:image/png;base64,$IMAGE_DATA"
            },
            {
              "type": "input_text",
              "text": "Transform this image into a watercolor painting."
            }
          ]
        }
      ],
      "provider_options": {
        "image": {
          "height": 512,
          "width": 512,
          "steps": 28
        }
      }
    }
    EOF
    ```

    Send the request and decode the returned image data:

    ```bash theme={null}
    curl -X POST https://api.modular.com/v1/responses \
      -H "Authorization: Bearer <your-api-key>" \
      -H "Content-Type: application/json" \
      --data-binary @request.json \
      | jq -r '.output[0].content[0].image_data' \
      | base64 -d > output-image-to-image.png
    ```
  </Tab>
</Tabs>

The transformed image should look similar to the following:

<img src="https://mintcdn.com/modular/21dnEKVIFHhCZ_2p/inference/images/output-image-to-image.png?fit=max&auto=format&n=21dnEKVIFHhCZ_2p&q=85&s=2d2316c0e1298de10432d9cf393a64ca" alt="Image-to-image output: the mountain landscape transformed into a watercolor painting." width="1024" height="1024" data-path="inference/images/output-image-to-image.png" />
