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

# GPT Image Generation API | OpenAI-Compatible Image API | LaoZhang API

> High-quality image generation using OpenAI official Image API format with GPT-Image-1 model - fully compatible unified API gateway with 200+ models

## Model Overview

GPT-Image-1 is OpenAI's latest image generation model, providing high-quality image generation capabilities. This documentation explains how to use the standard OpenAI Image API format through LaoZhang API to access this model.

## Introduction

GPT-Image-1 supports generating high-quality images from text prompts, fully compatible with OpenAI's official Image Generation API format.

### Key Features

* **High-Quality Image Generation**: Generate detailed images that match your prompts
* **Fast Response**: Optimized generation speed for quick results
* **Pay-as-you-go Pricing**: Input \$10/M Tokens, Output \$40/M Tokens
* **Fully Compatible**: 100% compatible with OpenAI Image API format

## Quick Start

### Basic Configuration

```python theme={null}
import openai

{/* Configure LaoZhang API endpoint and key */}
openai.api_base = "https://api2.laozhang.ai/v1"
openai.api_key = "your-api-key"
```

### Generate Images

Using standard OpenAI Image API format:

```python theme={null}
{/* Generate image */}
response = openai.Image.create(
    model="gpt-image-1",
    prompt="A serene landscape with mountains and a lake at sunset",
    n=1,
    size="1024x1024"
)

{/* Get generated image URL */}
image_url = response['data'][0]['url']
print(f"Generated image URL: {image_url}")
```

## API Parameters

### Request Parameters

| Parameter            | Type    | Required | Description                                                                  |
| -------------------- | ------- | -------- | ---------------------------------------------------------------------------- |
| `model`              | string  | Yes      | Model name, use `gpt-image-1`                                                |
| `prompt`             | string  | Yes      | Text prompt for image generation, max 1000 characters                        |
| `n`                  | integer | No       | Number of images to generate, default 1, max 10                              |
| `size`               | string  | No       | Image size, supports `1024x1024`, `1536x1024`, `1024x1536`, `auto` (default) |
| `quality`            | string  | No       | Render quality, supports `low`, `medium`, `high`, `auto` (default)           |
| `output_format`      | string  | No       | Output format, supports `png` (default), `jpeg`, `webp`                      |
| `output_compression` | integer | No       | Compression level (0-100%), applies to JPEG and WebP only                    |
| `background`         | string  | No       | Background setting, supports `transparent`, `opaque`, `auto` (default)       |
| `response_format`    | string  | No       | Return format, `url` (default) or `b64_json`                                 |
| `user`               | string  | No       | User identifier for monitoring and abuse detection                           |

### Parameter Details

#### Size Options (size)

* `1024x1024` - Square (fastest generation by default)
* `1536x1024` - Landscape mode
* `1024x1536` - Portrait mode
* `auto` - Model automatically selects optimal size based on prompt (default)

#### Quality Options (quality)

* `low` - Low quality, fastest generation
* `medium` - Medium quality
* `high` - High quality, most detailed
* `auto` - Model automatically selects based on prompt (default)

<Tip>
  **Performance Tip**: Square images + standard quality generate fastest. For latency-sensitive applications, use `jpeg` format instead of `png`.
</Tip>

#### Output Format (output\_format)

* `png` - Lossless compression, default format
* `jpeg` - Lossy compression, supports compression levels, faster generation
* `webp` - Modern format, smaller files, supports compression levels

When using `jpeg` or `webp`, control compression level with `output_compression` parameter (0-100%). For example, `output_compression=50` compresses the image by 50%.

#### Background Options (background)

* `transparent` - Transparent background (for PNG/WebP)
* `opaque` - Opaque background
* `auto` - Model automatically selects (default)

### Response Format

```json theme={null}
{
  "created": 1702486395,
  "data": [
    {
      "url": "https://..."
    }
  ]
}
```

## Usage Examples

### Python Example

```python theme={null}
import openai
import requests
from PIL import Image
from io import BytesIO

{/* Configuration */}
openai.api_base = "https://api2.laozhang.ai/v1"
openai.api_key = "your-api-key"

{/* Generate image with new parameters */}
def generate_image(prompt, size="auto", quality="auto", output_format="png", n=1):
    try:
        response = openai.Image.create(
            model="gpt-image-1",
            prompt=prompt,
            n=n,
            size=size,
            quality=quality,
            output_format=output_format
        )

        {/* Return image URLs */}
        return [item['url'] for item in response['data']]

    except Exception as e:
        print(f"Error generating image: {e}")
        return None

{/* Advanced example: using compression and background settings */}
def generate_advanced_image(prompt, compression=None):
    params = {
        "model": "gpt-image-1",
        "prompt": prompt,
        "size": "1536x1024",  {/* Landscape image */}
        "quality": "high",
        "output_format": "jpeg" if compression else "png",
        "background": "transparent"
    }

    {/* Add compression parameter if needed */}
    if compression:
        params["output_compression"] = compression

    try:
        response = openai.Image.create(**params)
        return response['data'][0]['url']
    except Exception as e:
        print(f"Error: {e}")
        return None

{/* Download and save image */}
def save_image(url, filename):
    response = requests.get(url)
    img = Image.open(BytesIO(response.content))
    img.save(filename)
    print(f"Image saved as {filename}")

{/* Usage example */}
prompts = [
    "A futuristic city with flying cars and neon lights",
    "A cozy coffee shop in autumn with warm lighting",
    "An abstract art piece with vibrant colors and geometric shapes"
]

for i, prompt in enumerate(prompts):
    print(f"Generating: {prompt}")
    urls = generate_image(prompt)
    if urls:
        save_image(urls[0], f"image_{i+1}.png")
```

### Node.js Example

```javascript theme={null}
const OpenAI = require('openai');
const fs = require('fs');
const https = require('https');

{/* Initialize client */}
const openai = new OpenAI({
  apiKey: 'your-api-key',
  baseURL: 'https://api2.laozhang.ai/v1'
});

{/* Generate image with new parameters */}
async function generateImage(prompt, options = {}) {
  try {
    const response = await openai.images.generate({
      model: "gpt-image-1",
      prompt: prompt,
      n: options.n || 1,
      size: options.size || "auto",
      quality: options.quality || "auto",
      output_format: options.format || "png",
      background: options.background || "auto",
      ...options.compression && { output_compression: options.compression }
    });

    return response.data[0].url;
  } catch (error) {
    console.error('Error generating image:', error);
    return null;
  }
}

{/* Advanced example: generate high-quality landscape image */}
async function generateLandscapeImage(prompt) {
  const options = {
    size: "1536x1024",
    quality: "high",
    format: "jpeg",
    compression: 85  {/* 85% quality */}
  };

  return await generateImage(prompt, options);
}

{/* Download image */}
function downloadImage(url, filepath) {
  return new Promise((resolve, reject) => {
    const file = fs.createWriteStream(filepath);
    https.get(url, (response) => {
      response.pipe(file);
      file.on('finish', () => {
        file.close();
        console.log(`Image saved to ${filepath}`);
        resolve();
      });
    }).on('error', reject);
  });
}

{/* Usage example */}
async function main() {
  const prompt = "A beautiful Japanese garden with cherry blossoms";
  const imageUrl = await generateImage(prompt);

  if (imageUrl) {
    await downloadImage(imageUrl, 'japanese_garden.png');
  }
}

main();
```

### cURL Example

```bash theme={null}
curl https://api2.laozhang.ai/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "gpt-image-1",
    "prompt": "A white siamese cat sitting on a windowsill",
    "n": 1,
    "size": "1024x1024"
  }'
```

## Prompt Optimization Tips

### 1. Detailed Descriptions

Provide specific, detailed descriptions for better results:

```python theme={null}
{/* Basic prompt */}
prompt_basic = "a cat"

{/* Optimized prompt */}
prompt_detailed = "a fluffy white Persian cat sitting on a vintage velvet cushion, soft natural lighting, professional photography, shallow depth of field"
```

### 2. Style Specification

Explicitly specify desired artistic style:

```python theme={null}
styles = [
    "in the style of Studio Ghibli anime",
    "photorealistic, professional photography",
    "digital art, concept art style",
    "oil painting, impressionist style",
    "minimalist, flat design illustration"
]
```

### 3. Composition and Perspective

Specifying composition and perspective yields more precise results:

```python theme={null}
compositions = [
    "close-up portrait, centered composition",
    "wide angle landscape shot, rule of thirds",
    "aerial view, bird's eye perspective",
    "low angle shot, dramatic perspective"
]
```

## Batch Generation

Example of batch image generation:

```python theme={null}
import asyncio
import aiohttp
import openai

async def generate_batch_images(prompts, size="1024x1024"):
    """Batch generate images"""
    tasks = []

    async with aiohttp.ClientSession() as session:
        for prompt in prompts:
            task = generate_single_image(session, prompt, size)
            tasks.append(task)

        results = await asyncio.gather(*tasks)

    return results

async def generate_single_image(session, prompt, size):
    """Asynchronously generate single image"""
    headers = {
        "Authorization": f"Bearer {openai.api_key}",
        "Content-Type": "application/json"
    }

    data = {
        "model": "gpt-image-1",
        "prompt": prompt,
        "n": 1,
        "size": size
    }

    async with session.post(
        f"{openai.api_base}/images/generations",
        headers=headers,
        json=data
    ) as response:
        result = await response.json()
        return {
            "prompt": prompt,
            "url": result['data'][0]['url']
        }

{/* Usage example */}
prompts = [
    "A majestic eagle soaring over mountains",
    "An underwater coral reef teeming with colorful fish",
    "A cozy cabin in a snowy forest at night",
    "A bustling Tokyo street with neon signs"
]

{/* Run batch generation */}
results = asyncio.run(generate_batch_images(prompts))
for result in results:
    print(f"Prompt: {result['prompt']}")
    print(f"URL: {result['url']}\n")
```

## Error Handling

### Common Error Codes

| Error Code | Description                | Solution                                 |
| ---------- | -------------------------- | ---------------------------------------- |
| 400        | Invalid request parameters | Check prompt length and parameter format |
| 401        | Authentication failed      | Verify API key is correct                |
| 429        | Too many requests          | Reduce request frequency or upgrade plan |
| 500        | Server error               | Retry later or contact support           |

### Error Handling Example

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

client = OpenAI(
    api_key="your-api-key",
    base_url="https://api2.laozhang.ai/v1"
)

def generate_with_retry(prompt, max_retries=3):
    """Image generation with retry mechanism"""
    for attempt in range(max_retries):
        try:
            response = client.images.generate(
                model="gpt-image-1",
                prompt=prompt,
                n=1,
                size="1024x1024"
            )
            return response.data[0].url

        except Exception as e:
            if attempt < max_retries - 1:
                wait_time = 2 ** attempt  {/* Exponential backoff */}
                print(f"Error: {e}. Retrying in {wait_time} seconds...")
                time.sleep(wait_time)
            else:
                print(f"Failed after {max_retries} attempts: {e}")
                return None
```

## Best Practices

### 1. Prompt Length Control

While supporting up to 1000 characters, detailed descriptions of 100-200 characters usually produce great results.

### 2. Image Size and Quality Selection

**Size Recommendations**:

* `1024x1024`: General use, fastest generation
* `1536x1024`: Landscape composition, suitable for scenery, banners
* `1024x1536`: Portrait composition, suitable for portraits, posters
* `auto`: Let model auto-select based on content

**Quality Recommendations**:

* `low`: Quick preview, batch generation
* `medium`: Daily use
* `high`: Professional use, print output
* `auto`: Model auto-optimization

**Format Selection**:

* `png`: Need transparent background or lossless quality
* `jpeg`: Prioritize speed and smaller files
* `webp`: Modern web apps, best compression ratio

### 3. Content Moderation

Generated images are automatically moderated to ensure compliance with usage policies. Avoid requesting generation of:

* Violent or disturbing images
* Adult content
* Hateful or discriminatory content
* Misleading or false information
* Copyright-infringing content

### 4. Cost Optimization

* Use smaller sizes for testing and preview
* Set reasonable concurrency for batch generation
* Cache generated image URLs to avoid regeneration

## Difference from Sora Image

| Feature         | GPT-Image-1                                | Sora Image           |
| --------------- | ------------------------------------------ | -------------------- |
| Pricing         | Token-based (\$10 input/\$40 output per M) | \$0.01/image         |
| Billing Method  | Pay-as-you-go                              | Per-image            |
| API Type        | Images API                                 | Chat Completions API |
| Size Support    | 256x256, 512x512, 1024x1024                | 2:3, 3:2, 1:1 ratios |
| Response Format | URL or Base64                              | Markdown image link  |

## FAQ

### Q: Can I use generated images commercially?

A: Yes, you own full rights to images generated via API and can use them commercially.

### Q: How long are image URLs valid?

A: Generated image URLs are typically valid for 24 hours. Download and save promptly.

### Q: Are Chinese prompts supported?

A: Supported, but English prompts are recommended for best results.

### Q: How to improve generated image quality?

A: Use detailed, specific descriptions including style, lighting, composition details.

## Related Resources

* [OpenAI Official Image Generation Guide](https://platform.openai.com/docs/guides/images)
* [LaoZhang API Console](https://api2.laozhang.ai)
* [Technical Support](mailto:hi@laozhang.ai)

<Note>
  GPT-Image-1 uses token-based billing (Input \$10/M Tokens, Output \$40/M Tokens), with actual cost depending on prompt length and response content. Compared to fixed-price Sora Image (\$0.01/image), it's better suited for scenarios requiring flexible cost control.
</Note>
