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

# Generate Images

Creates an image given a prompt.

## Method Signature

```python theme={null}
client.images.generate(
    prompt="A cute cat",
    model="dall-e-3",
    size="1024x1024",
    quality="hd",
    n=1,
    response_format="url",
    style="vivid"
)
```

## Parameters

<ParamField path="prompt" type="string" required>
  A text description of the desired image(s). The maximum length is:

  * 32000 characters for GPT image models
  * 4000 characters for `dall-e-3`
  * 1000 characters for `dall-e-2`
</ParamField>

<ParamField path="model" type="string">
  The model to use for image generation. Options:

  * `dall-e-2`
  * `dall-e-3`
  * `gpt-image-1`
  * `gpt-image-1-mini`
  * `gpt-image-1.5`
  * `chatgpt-image-latest`

  Defaults to `dall-e-2` unless a parameter specific to the GPT image models is used.
</ParamField>

<ParamField path="n" type="integer">
  The number of images to generate. Must be between 1 and 10. For `dall-e-3`, only `n=1` is supported.
</ParamField>

<ParamField path="quality" type="string">
  The quality of the image that will be generated:

  * `auto` (default) - automatically select the best quality for the given model
  * `high`, `medium`, `low` - supported for GPT image models
  * `hd`, `standard` - supported for `dall-e-3`
  * `standard` - only option for `dall-e-2`
</ParamField>

<ParamField path="response_format" type="string">
  The format in which the generated images are returned. Must be one of `url` or `b64_json`. URLs are only valid for 60 minutes after the image has been generated.

  This parameter is only supported for `dall-e-2` and `dall-e-3` - GPT image models always return base64-encoded images.
</ParamField>

<ParamField path="size" type="string">
  The size of the generated images:

  * GPT image models: `1024x1024`, `1536x1024` (landscape), `1024x1536` (portrait), or `auto` (default)
  * `dall-e-2`: `256x256`, `512x512`, or `1024x1024`
  * `dall-e-3`: `1024x1024`, `1792x1024`, or `1024x1792`
</ParamField>

<ParamField path="style" type="string">
  The style of the generated images. Only supported for `dall-e-3`:

  * `vivid` - generates hyper-real and dramatic images
  * `natural` - produces more natural, less hyper-real looking images
</ParamField>

<ParamField path="background" type="string">
  Allows to set transparency for the background of the generated image(s). Only supported for GPT image models:

  * `transparent`
  * `opaque`
  * `auto` (default) - model automatically determines the best background

  If `transparent`, the output format needs to support transparency (`png` or `webp`).
</ParamField>

<ParamField path="output_format" type="string">
  The format in which the generated images are returned. Only supported for GPT image models:

  * `png` (default)
  * `jpeg`
  * `webp`
</ParamField>

<ParamField path="output_compression" type="integer">
  The compression level (0-100%) for the generated images. Only supported for GPT image models with `webp` or `jpeg` output formats. Defaults to 100.
</ParamField>

<ParamField path="moderation" type="string">
  Control the content-moderation level for images generated by the GPT image models:

  * `auto` (default)
  * `low` - less restrictive filtering
</ParamField>

<ParamField path="stream" type="boolean">
  Generate the image in streaming mode. Defaults to `false`. Only supported for GPT image models. See the [Image generation guide](https://platform.openai.com/docs/guides/image-generation) for more information.
</ParamField>

<ParamField path="partial_images" type="integer">
  The number of partial images to generate. Used for streaming responses that return partial images. Value must be between 0 and 3. When set to 0, the response will be a single image sent in one streaming event.

  Note that the final image may be sent before the full number of partial images are generated if the full image is generated more quickly.
</ParamField>

<ParamField path="user" type="string">
  A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
</ParamField>

## Returns

Returns an `ImagesResponse` object containing the generated images.

<ResponseField name="created" type="integer">
  The Unix timestamp (in seconds) of when the image was created.
</ResponseField>

<ResponseField name="data" type="array">
  The list of generated images.

  <Expandable title="Image object">
    <ResponseField name="url" type="string">
      The URL of the generated image (for `dall-e-2` and `dall-e-3` with `response_format=url`). URLs are only valid for 60 minutes.
    </ResponseField>

    <ResponseField name="b64_json" type="string">
      The base64-encoded JSON of the generated image. Returned by default for GPT image models, or when `response_format=b64_json` for DALL-E models.
    </ResponseField>

    <ResponseField name="revised_prompt" type="string">
      For `dall-e-3` only, the revised prompt that was used to generate the image.
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="usage" type="object">
  For `gpt-image-1` only, the token usage information for the image generation.
</ResponseField>

## Examples

### Generate with DALL-E 3 (URL response)

```python theme={null}
from openai import OpenAI
client = OpenAI()

response = client.images.generate(
    model="dall-e-3",
    prompt="A futuristic cityscape at sunset",
    size="1024x1024",
    quality="hd",
    n=1
)

image_url = response.data[0].url
print(f"Image URL: {image_url}")
```

### Generate with DALL-E 2 (base64 response)

```python theme={null}
import base64
from openai import OpenAI

client = OpenAI()

response = client.images.generate(
    model="dall-e-2",
    prompt="A white siamese cat",
    size="512x512",
    response_format="b64_json",
    n=2
)

for i, image in enumerate(response.data):
    image_data = base64.b64decode(image.b64_json)
    with open(f"image_{i}.png", "wb") as f:
        f.write(image_data)
```

### Generate with GPT Image model

```python theme={null}
from openai import OpenAI
client = OpenAI()

response = client.images.generate(
    model="gpt-image-1.5",
    prompt="A minimalist logo design for a tech startup",
    size="1024x1024",
    quality="high",
    background="transparent",
    output_format="png"
)

# GPT image models always return base64
import base64
image_data = base64.b64decode(response.data[0].b64_json)
with open("logo.png", "wb") as f:
    f.write(image_data)
```

### Streaming generation

```python theme={null}
from openai import OpenAI
client = OpenAI()

stream = client.images.generate(
    model="gpt-image-1",
    prompt="A scenic mountain landscape",
    stream=True,
    partial_images=2
)

for event in stream:
    if event.type == "partial_image":
        print(f"Received partial image at {event.partial_image.progress}% progress")
    elif event.type == "completed":
        print("Image generation completed!")
        image_data = event.data[0].b64_json
```
