调用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-6.1 Terra 正式上线
gpt·ai·openai·codex
星际码仔42 分钟前
Codex Co²:AI 时代的结对编程,应该是怎样的?
openai·ai编程·vibecoding
ynchyong1 小时前
OpenAI 技术全景解析
openai·rag·function·guardrails
sg_knight5 小时前
Bug 定位实战:把报错丢给 Codex,它是怎么修的
chatgpt·llm·bug·openai·agent·ai编程·codex
threerocks1 天前
妈妈都分不清的配音 | VoiceStudio + VoxCPM 2 本地配音保姆级教程
aigc·openai·ai编程
陆映质1 天前
千种姿势生图节点教程:把 1,439 张人体线稿做成 ComfyUI 节点,一张参考图锁定动作生成完整人物照
openai
莪_幻尘1 天前
Codex 周活涨 6 倍背后:AI Agent 的四条战线
llm·openai
vipxieliang1 天前
数字员工上岗实录:各行业 Agent 部署案例集
openai
墨林陌1 天前
AI 热点日报(2026-10-30):新芯航途发布 700 TOPS 大模型芯片,微软发债 250 亿加码 AI,谷歌财报现现金流红灯
openai
deepseek232 天前
OpenAI三项科学发现拆解:3-SAT亚指数算法挑战ETH,AI科学声明的验证流水线怎么搭
人工智能·算法·openai