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

输出:

相关推荐
码农BookSea5 小时前
为什么ChatGPT能听懂你说的话?Embedding技术揭秘
后端·openai
少林码僧8 小时前
2.5 学术界的“GPT”:DeepResearch 深度研究助手从零到一创建与配置指南
aigc·openai·ai编程
Lei活在当下8 小时前
【Part 1】Harness Engineering 对程序员来说意味着什么?
chatgpt·openai·ai编程
摆烂工程师8 小时前
Sora 还是关了:最像未来的 AI 产品,为什么先死了?
openai·视频编码·sora
安思派Anspire11 小时前
OpenClaw的爆发:OpenClaw如何催生了整个开源AI智能体生态系统
aigc·openai
墨渊君15 小时前
从 0 到 1:用 Node 打通 OpenClaw WebSocket 通信全流程
前端·openai·agent
Soari3 天前
OpenAI Codex国内使用教程
openai·codex
Trouvaille ~3 天前
零基础入门 LangChain 与 LangGraph(三):环境搭建、包安装与第一个 LangChain 程序
python·ai·chatgpt·langchain·大模型·openai·langgraph
掘金一周5 天前
每月固定续订,但是token根本不够用,掘友们有无算力焦虑啊 | 沸点周刊 4.2
前端·aigc·openai
安思派Anspire5 天前
Github上全球趋势榜第一的产品,背后是Anspire Search的搜索硬实力!
aigc·openai