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

输出:

相关推荐
机器之心1 天前
让谷歌翻身的Gemini 3,上线Flash版
人工智能·openai
梁辰兴1 天前
OpenAI更新ChatGPT Images:生成速度最高提升4倍,原生多模态模型
人工智能·科技·ai·chatgpt·大模型·openai·图像生成
恋猫de小郭1 天前
OpenAI :你不需要跨平台框架,只需要在 Android 和 iOS 上使用 Codex
android·前端·openai
oden2 天前
AI服务商切换太麻烦?一个AI Gateway搞定监控、缓存和故障转移(成本降40%)
后端·openai·api
机器之心2 天前
OpenAI推出全新ChatGPT Images,奥特曼亮出腹肌搞宣传
人工智能·openai
机器之心2 天前
SIGGRAPH Asia 2025:摩尔线程赢图形顶会3DGS挑战赛大奖,自研LiteGS全面开源
人工智能·openai
安思派Anspire2 天前
AI智能体:完整课程(初级)
aigc·openai·agent
树獭叔叔3 天前
模型编辑 vs 参数微调:给零算法基础AI从业者的讲解
aigc·openai·ai编程
机器之心3 天前
国产芯片也能跑AI视频实时生成了,商汤Seko 2.0揭秘幕后黑科技
人工智能·openai
机器之心3 天前
Thinking Machines首款产品重大更新:K2 Thinking、Qwen3-VL都可以微调了
人工智能·openai