01Python 快速开始
安装 OpenAI Python SDK,然后设置自定义 `base_url`。
from openai import OpenAI
import base64
client = OpenAI(
api_key="sk-img-你的APIKey",
base_url="https://api.imagezh.com/v1"
)
result = client.images.generate(
model="gpt-image-2",
prompt="极简产品摄影,一瓶透明香水放在水面上",
size="1024x1024",
quality="high"
)
image_bytes = base64.b64decode(result.data[0].b64_json)
with open("image.png", "wb") as file:
file.write(image_bytes)
02cURL 文生图
curl https://api.imagezh.com/v1/images/generations \
-H "Authorization: Bearer sk-img-你的APIKey" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "电影感的上海雨夜,霓虹灯倒映在街道上",
"size": "1536x1024",
"quality": "high",
"output_format": "png"
}'
03图片编辑
使用 multipart/form-data 上传参考图,并描述希望修改的内容。
curl https://api.imagezh.com/v1/images/edits \
-H "Authorization: Bearer sk-img-你的APIKey" \
-F "model=gpt-image-2" \
-F "image=@input.png" \
-F "prompt=保留主体,把背景替换成日落海边" \
-F "quality=high"
04常用参数
model`gpt-image-2`当前图片模型。
size`auto` / 方形 / 横向 / 纵向常见值包括 1024x1024、1536x1024、1024x1536。
quality`auto` / `low` / `medium` / `high`质量越高通常耗时越长;本站当前仍按成功图片张数计费。
output_format`png` / `jpeg` / `webp`按业务场景选择输出格式。
05查询当前额度
curl https://api.imagezh.com/api/image-api-key/me \
-H "Authorization: Bearer sk-img-你的APIKey"
不要在浏览器前端、公开仓库或客户端安装包中暴露 API Key。服务端调用时请使用环境变量保存。