注册

python 使用gpt-image-1 创建编辑图片

创建图片

from openai import OpenAI
import base64

client = OpenAI(
    base_url="https://api.quickrouter.ai/v1",
    api_key="YOUR_API_KEY"
)

prompt = "A children's book illustration of a veterinarian using a stethoscope to listen to an otter pup's heartbeat."

result = client.images.generate(
    model="gpt-image-1",
    prompt=prompt
)

image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)

with open("otter.png", "wb") as f:
    f.write(image_bytes)

编辑图片

from openai import OpenAI
import base64

client = OpenAI(
    base_url="https://api.quickrouter.ai/v1",
    api_key="YOUR_API_KEY"
)

prompt = "Generate a hyperrealistic image of a gift basket on a white background with 'Relax' written on it, with a handwritten-style font. The basket should contain all items from the reference images."

result = client.images.edit(
    model="gpt-image-1",
    image=[
        open("body-lotion.png", "rb"),
        open("bath-bomb.png", "rb"),
        open("incense-kit.png", "rb"),
        open("soap.png", "rb"),
    ],
    prompt=prompt
)

image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)

with open("gift-basket.png", "wb") as f:
    f.write(image_bytes)