调用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 小时前
Give azure openai an encyclopedia of information
ai·openai·azure
Snowbowღ5 小时前
OpenAI / GPT-4o:Python 返回结构化 / JSON 输出
python·json·openai·api·gpt-4o·pydantic·结构化输出
营赢盈英20 小时前
Allow anonymous access to my Azure OpenAI chat bot
ai·openai·azure·webapps
网络研究院2 天前
解码 OpenAI 的 o1 系列大型语言模型
人工智能·语言模型·自然语言处理·openai·技术·推理·能力
营赢盈英2 天前
OpenAI GPT-3 API error: “You must provide a model parameter“
chatgpt·gpt-3·openai·swift
营赢盈英2 天前
OpenAI API key not working in my React App
javascript·ai·openai·reactjs·chatbot
营赢盈英3 天前
Using OpenAI API from Firebase Cloud Functions in flutter app
ai·node.js·openai·googlecloud·firebase
营赢盈英5 天前
404 error when doing workload anlysis using locust on OpenAI API (GPT.35)
人工智能·python·openai·locust
CaiYongji5 天前
深度!程序员生涯的垃圾时间(上)
人工智能·gpt·chatgpt·openai
营赢盈英6 天前
How to see if openAI (node js) createModeration response “flagged“ is true
javascript·ai·node.js·openai·api