调用DALL·E-3 API生成图片

python 复制代码
import base64
import matplotlib.pyplot as plt
from openai import OpenAI
from PIL import Image
from io import BytesIO

api_key = "sk-xxxxx"

def base64_to_image(base64_string):
    try:
        image_data = base64.b64decode(base64_string)
        image_buffer = BytesIO(image_data)
        image = Image.open(image_buffer)
        return image
    except Exception as e:
        print(f"An error occurred: {e}")
        return None

client = OpenAI(api_key=api_key)
response = client.images.generate(
    model="dall-e-3",
    prompt="A spaceship flying through the universe", # an example
    size="1024x1024",
    quality="standard",
    n=1,
    response_format='b64_json'
)

image_b64 = response.data[0].b64_json
generated_image = base64_to_image(image_b64)

if generated_image:
	plt.imshow(generated_image)
	plt.axis("off")
	plt.show()

输出:

相关推荐
大千AI助手2 小时前
Text-Embedding-Ada-002:技术原理、性能评估与应用实践综述
人工智能·机器学习·openai·embedding·ada-002·文本嵌入·大千ai助手
win4r15 小时前
🚀GPT-5.2又赢了?我用一套“开发者视角”的实测流程,真实能力一看便知!
openai·ai编程·vibecoding
算家计算17 小时前
突然发布!GPT-5.2深夜来袭,3个版本碾压人类专家,打工人该怎么选?
算法·openai·ai编程
哪吒编程17 小时前
重磅更新!OpenAI最新模型GPT 5.2上线
openai
安思派Anspire1 天前
构建自我提升的AI智能体:完整训练架构指南
aigc·openai·agent
用户6600676685391 天前
AI图片生成实战:Node.js + OpenAI DALL·E 3
aigc·openai
没逻辑1 天前
让每一次AI对话都精准高效 —— Prompt设计六要素
openai·ai编程
新智元2 天前
GPT-5.2 提前泄露?今夜,OpenAI 要拿 Gemini 3 祭天!
人工智能·openai
_一两风2 天前
揭秘 ChatGPT 同款“打字机”特效:前端流式输出 (Streaming) 原理全解
前端·vue.js·openai
安思派Anspire2 天前
麻省理工学院的“冰山计划”揭示了AI对就业的影响远比表面上看起来要大得多
aigc·openai·agent