GPT-Image-1 is OpenAI’s latest image generation model, providing high-quality image generation capabilities. This document describes how to use this model through APIYI using the standard OpenAI Image API format.
Overview
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: Creates detailed images that match your prompts
- 🚀 Fast Response: Optimized generation speed for quick results
- 💰 Token-Based Billing: 10/Minputtokens,40/M output tokens
- 🔧 Full Compatibility: 100% compatible with OpenAI Image API format
Quick Start
Basic Configuration
import openai
# Configure APIYI endpoint and key
openai.api_base = "https://api.apiyi.com/v1"
openai.api_key = "your-api-key"
Generate Images
Call using standard OpenAI Image API format:
# 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 | Rendering 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/horizontal mode
1024x1536 - Portrait/vertical mode
auto - Model automatically selects best 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)
Performance Tip: Square images + standard quality generate fastest. For latency-sensitive applications, use jpeg format instead of png.
png - Lossless compression, default format
jpeg - Lossy compression, supports compression level, faster generation
webp - Modern format, smaller files, supports compression level
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)
{
"created": 1702486395,
"data": [
{
"url": "https://..."
}
]
}
Usage Examples
Python Example
import openai
import requests
from PIL import Image
from io import BytesIO
# Configuration
openai.api_base = "https://api.apiyi.com/v1"
openai.api_key = "your-api-key"
# Generate image (using 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
const OpenAI = require('openai');
const fs = require('fs');
const https = require('https');
// Initialize client
const openai = new OpenAI({
apiKey: 'your-api-key',
baseURL: 'https://api.apiyi.com/v1'
});
// Generate image (using 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
curl https://api.apiyi.com/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:
# 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 the desired artistic style:
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:
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 generating multiple images in batch:
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",
"A 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 technical support |
Error Handling Example
import time
from openai import OpenAI
client = OpenAI(
api_key="your-api-key",
base_url="https://api.apiyi.com/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 max 1000 characters is supported, typically 100-200 characters of detailed description yields excellent results.
2. Image Size and Quality Selection
Size Recommendations:
1024x1024: General scenarios, fastest generation
1536x1024: Landscape composition, suitable for scenery, banners
1024x1536: Portrait composition, suitable for portraits, posters
auto: Let model automatically choose based on content
Quality Recommendations:
low: Quick preview, batch generation
medium: Daily use
high: Professional use, print output
auto: Model automatic optimization
Format Selection:
png: Need transparent background or lossless quality
jpeg: Prioritize speed and smaller files
webp: Modern web applications, best compression ratio
3. Content Moderation
Generated images are automatically moderated to ensure compliance with usage policies. Avoid requesting generation of:
- Violent or disturbing imagery
- Adult content
- Hateful or discriminatory content
- Misleading or false information
- Copyright-infringing content
4. Cost Optimization
- Use smaller sizes for testing and previewing
- Set reasonable concurrency limits for batch generation
- Cache generated image URLs to avoid duplicate generation
Comparison with Sora Image
| Feature | GPT-Image-1 | Sora Image |
|---|
| Pricing | Token-based (10input/40 output per M) | $0.01/image |
| Billing Method | Pay-per-use | Pay-per-request |
| 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 formatted image link |
FAQ
Q: Can generated images be used commercially?
A: Yes, you own full rights to images generated via the API and can use them for commercial purposes.
Q: How long are image URLs valid?
A: Generated image URLs are typically valid for 24 hours. We recommend downloading and saving them promptly.
Q: Are Chinese prompts supported?
A: Yes, but English prompts are recommended for best results.
Q: How to improve generated image quality?
A: Use detailed, specific descriptions including style, lighting, composition, and other details.
GPT-Image-1 uses token-based billing (10/Minputtokens,40/M output 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.