调用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()

输出:

相关推荐
新智元12 小时前
GPT-5.2 提前泄露?今夜,OpenAI 要拿 Gemini 3 祭天!
人工智能·openai
_一两风14 小时前
揭秘 ChatGPT 同款“打字机”特效:前端流式输出 (Streaming) 原理全解
前端·vue.js·openai
安思派Anspire16 小时前
麻省理工学院的“冰山计划”揭示了AI对就业的影响远比表面上看起来要大得多
aigc·openai·agent
机器之心2 天前
「豆包手机」为何能靠超级Agent火遍全网,我们听听AI学者们怎么说
人工智能·openai
机器之心2 天前
一手实测 | 智谱AutoGLM重磅开源: AI手机的「安卓时刻」正式到来
人工智能·openai
安思派Anspire2 天前
麦肯锡刚刚发布了他们的2025年AI报告。以下是TLDR
aigc·openai·agent
Sherlock Ma2 天前
OpenAI新论文!GPT-5-Thinking新训练方法
人工智能·gpt·深度学习·语言模型·自然语言处理·chatgpt·openai
新智元2 天前
OpenAI最新报告曝光!前5%精英效率暴涨16倍,普通人却被悄悄淘汰
人工智能·openai
新智元2 天前
清华紧逼谷歌,AI顶会NeurIPS论文数第二!中国占半壁江山
人工智能·openai
组合缺一2 天前
Solon AI 开发学习19 - 结合 Solon Flow 定制 ReAct 效果
java·人工智能·学习·ai·llm·openai·solon