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

输出:

相关推荐
XinZong12 小时前
【AI社交Skill】禁止人类发言”的AI闭环社交社区_ 到底什么是 clawreach 虾聊?
aigc·openai·ai编程
沸点小助手1 天前
「节前摸鱼实录 & AI 抢我饭碗的瞬间」沸点获奖名单公示|本周互动话题上新🎊
openai·沸点
Carson带你学Android1 天前
谁才是地表最强 Android Agent 大模型?Google官方测评来了!
android·openai
Lazy_zheng1 天前
用 Python 接入大模型 API:从 0 到 1 实现文本分类/抽取/匹配
llm·openai·agent
小兵张健2 天前
Codex 使用教程(2):设置与项目配置详解
程序员·openai·ai编程
遇见火星2 天前
OpenAI Codex 使用教程
ai·openai·codex
灵机一物2 天前
灵机一物AI原生电商小程序、PC端(已上线)-GPT-5.5 深度技术实测评测:Agent能力全面爆发,Codex重构开发范式,实测碾压Opus 4.7
openai·ai编程·gpt5.5·大模型技术评测
easyllm2 天前
GPT-5.5 全系上架 NoneLinear
gpt·openai·ai编程·智能体·大模型api·新模型上架·gpt5.5
叶子Talk2 天前
GPT-Image-2正式发布:文字渲染99%,Image Arena三项第一,AI图像生成彻底变天了
人工智能·gpt·计算机视觉·ai·openai·图像生成·gpt-image-2
程序员老赵3 天前
Docker 部署 Open WebUI + Ollama 完整教程(Windows / Linux 通用)—— 打造自己的本地OpenAI
aigc·openai·ai编程