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

输出:

相关推荐
沉默王二1 天前
官方点名,DeepSeek Harness 的 4 个神级插件。
openai·agent·ai编程
夕阳_醉了2 天前
Codex 客户端从装好到跑通:第三方 API 接入与配置避坑指南
人工智能·gpt·openai·codex·ai 编程
threerocks3 天前
一个人指挥一支 AI 剧组:Gemini 4 + Omni 1.2 剧情短片制作保姆级教程
aigc·openai·ai编程
东姬AI4 天前
挖出948个内核漏洞、自动写补丁:GPT-6 Cyber把AI推进“实战岗“,内容行业还卡在哪一关?
人工智能·网络安全·openai·gpt-6
LV技术派4 天前
我的《成为 AI Native Builder》开源小册,全量开放了!
aigc·openai·ai编程
ServBay5 天前
OpenAI大规模封号?ChatGPT 用户该如何应对
chatgpt·openai·ai编程
沉默王二5 天前
Hy4正式发布一手实测,腾讯这波起飞了。
openai·agent·vibecoding
小四的小六5 天前
重构 AI 代码,两千行里没有一处能删
aigc·openai·ai编程
vipxieliang5 天前
可观测性与安全的融合:统一数据平台的建设实践
运维·openai·自动化运维
liferecords7 天前
OpenAI 把「Critical」级挖洞模型交付出去了:GPT-6 Cyber 正式发布,越界率压到 1% 以下
网络安全·openai·ai安全·模型发布