Python image generation and editing with DALL-E and GPT-Image-2
Content updated: 2026-06-06
DALL-E 3 Image Generation
from openai import OpenAIclient = 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].urlprint(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
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
# 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)
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.
Image generation costs are relatively high. Test your prompt with smaller sizes first, then generate high-quality large images once satisfied.