pythonbeginner

OpenAI DALL-E Image Generation

Generate and save images using the DALL-E 3 API with quality and style control.

python
from openai import OpenAI
import base64
from pathlib import Path

client = OpenAI()

response = client.images.generate(
    model='dall-e-3',
    prompt='A futuristic data center with glowing blue server racks, photorealistic',
    size='1024x1024',
    quality='hd',
    style='natural',
    n=1,
    response_format='b64_json',
)

image_data = base64.b64decode(response.data[0].b64_json)
Path('output.png').write_bytes(image_data)
print('Image saved to output.png')
print(f'Revised prompt: {response.data[0].revised_prompt}')

Use Cases

  • AI image creation
  • content generation
  • marketing assets

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.