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

输出:

相关推荐
kfaino2 小时前
你好,我叫Token——AI世界里最忙的搬砖工
aigc·openai·ai编程
redreamSo9 小时前
大模型是不是到顶了?瓶颈到底在哪
人工智能·openai
牛奶15 小时前
连微软都用不起 AI 了
aigc·openai·ai编程
武子康17 小时前
调查研究-195 从 AmEx 支付系统看 Cell-based Architecture:真正的高可用,不是无限重试,而是控制失败边界
人工智能·openai·agent
程序员辉哥1 天前
Skill精通系列之Spec-Kit-最适合团队的SDD 开发框架
openai·ai编程·claude
秦瑜华1 天前
前端页面添加AI自动翻译按钮
前端·openai·ai编程
怕浪猫2 天前
第5章 AI Agent 工具使用:连接外部世界的桥梁
aigc·openai·ai编程
stormzhangV2 天前
给中文 AI 教程做了点微小的贡献
openai·ai编程·claude
武子康3 天前
调查研究-192 AI Agent 之间也需要“信任“:把多 Agent 信任变成可测指标
人工智能·openai·agent
武子康3 天前
调查研究-191 SenseVoice 不只是 ASR:把语音从“转文字“升级成“理解状态“
人工智能·深度学习·openai