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

输出:

相关推荐
空 白II1 小时前
9.17 大语言模型研究简报:openAI 公开模型失配报告框架
openai·大语言模型
ClouGence20 小时前
J人狂喜,P人救星:AI 帮你搞定十一假期旅行计划,一键生成共享网页
openai·agent
Lambert28120 小时前
9 月大模型榜单:第一名又换了,但开发者该看的不是它
aigc·openai
VIP_CQCRE1 天前
在 Visual Studio 里接入 Ace Data Cloud:用 LMLocal 快速获得 AI 编程能力
openai·ai编程·visual studio·ace data cloud·lmlocal
wangruofeng1 天前
AI 开始自己改进自己:智谱在十万卡集群上,让 AI 两周把效率提了 3 倍
aigc·openai·ai编程
9i编程2 天前
1. 把 DDD 开源脚手架化为自己的:先读懂它——Spring Boot 4.1 的四层架构、多数据源与领域事件
人工智能·openai·ai编程
七夜zippoe2 天前
Prompt 工程进阶:角色设定、任务分解、输出约束与示例引导
ai·prompt·openai·agent·工程进阶
Behaviour2 天前
Sam Altman 预热本周重磅产品发布,或为 GPT-6 Sol
人工智能·chatgpt·aigc·openai·vibecoding
全栈弄潮儿2 天前
AI 写代码前,我会让它先回答的 6 个问题
aigc·openai·ai编程
VIP_CQCRE2 天前
用 Ace Data Cloud 快速接入 OpenAI 语音识别:一行 base_url 改造,让音频转文字更简单
ai·openai·api·语音识别·acedatacloud