-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimage_generation.py
31 lines (29 loc) · 1.07 KB
/
image_generation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from openai import OpenAI
import os
client = OpenAI(
api_key=os.getenv("OPENAI_API_KEY"),
organization=os.getenv("OPENAI_ORG_ID"), # 可选 OpenAI API 中的组织 ID
timeout=30.0 # 默认超时时间
)
# 生成图像
def generate_image():
response = client.images.generate(
model="dall-e-3",
prompt="一只可爱的熊猫在吃竹子,背景是竹林,阳光透过竹叶",
n=1, # 生成图片数量
size="1024x1024", # 支持 1024x1024, 512x512, 256x256
quality="hd", # 图片质量:standard 或 hd
style="vivid", # natural 或 vivid
response_format="url" # url 或 b64_json
)
return response.data[0].url
# 图像变体
def create_image_variation():
response = client.images.create_variation(
image=open("panda.png", "rb"),
n=1, # 变体数量
size="1024x1024", # 输出尺寸
response_format="url", # 响应格式
user="user_123" # 用户标识符
)
return response.data[0].url