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

# Python Image Generation

> Python image generation and editing with DALL-E and GPT-Image-2

> Last updated: 2026-06-06

## DALL-E 3 Image Generation

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

client = OpenAI(api_key="sk-xxx", base_url="https://api.crazyrouter.com/v1")

response = client.images.generate(
    model="dall-e-3",
    prompt="A cat wearing a spacesuit walking on the moon, digital art style",
    size="1024x1024",
    quality="hd",
    n=1
)

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

### DALL-E Parameters

| Parameter | Options                               | Description      |
| --------- | ------------------------------------- | ---------------- |
| `size`    | `1024x1024`, `1792x1024`, `1024x1792` | Image dimensions |
| `quality` | `standard`, `hd`                      | Image quality    |
| `style`   | `vivid`, `natural`                    | Style            |

## GPT-Image-2 Image Generation

```python theme={null}
response = client.images.generate(
    model="gpt-image-2",
    prompt="A modern minimalist logo with an artificial intelligence theme",
    size="1024x1024",
    quality="low",
    output_format="png",
    n=1
)

print(response.data[0].url)
```

## GPT-Image-2 Image Editing

```python theme={null}
# Image editing (requires original image)
response = client.images.edit(
    model="gpt-image-2",
    image=open("original.png", "rb"),
    prompt="Change the background to a beach sunset",
    size="1024x1024",
    quality="low"
)

print(response.data[0].url)
```

## Download and Save Images

```python theme={null}
import requests

response = client.images.generate(
    model="dall-e-3",
    prompt="Cyberpunk city nightscape",
    size="1792x1024"
)

# Download image
image_url = response.data[0].url
img_data = requests.get(image_url).content
with open("cyberpunk_city.png", "wb") as f:
    f.write(img_data)
print("Image saved")
```

<Note>
  `gpt-image-2` usually returns `data[0].url`. Do not send `response_format` with `gpt-image-2`; use `output_format="png"`, `"jpeg"`, or `"webp"` to choose the image file format. For production image requests, use the primary route `https://api.crazyrouter.com/v1`.
</Note>

<Warning>
  Image generation costs are relatively high. Test your prompt with smaller sizes first, then generate high-quality large images once satisfied.
</Warning>
