docs: add T-619 vision task and image edit script
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
# POST https://xiaxiuxiu.com/v1/images/edits (OpenAI compatible)
|
||||
# Use the fixed source image and prompt file to generate one optimized image.
|
||||
import base64
|
||||
import os
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
from openai import OpenAI
|
||||
|
||||
|
||||
BASE_DIR = Path(__file__).resolve().parent
|
||||
SOURCE_IMAGE = BASE_DIR / "51513423876.jpg"
|
||||
PROMPT_FILE = BASE_DIR / "图生图提示词.txt"
|
||||
OUTPUT_IMAGE = BASE_DIR / "51513423876_new.jpg"
|
||||
TITLE = "韓系極簡V領扭結短袖上衣"
|
||||
|
||||
|
||||
api_key = os.environ.get("XIAXIUXIU_API_KEY", "").strip()
|
||||
if not api_key:
|
||||
raise RuntimeError("请先设置环境变量 XIAXIUXIU_API_KEY")
|
||||
if not SOURCE_IMAGE.is_file():
|
||||
raise FileNotFoundError(f"找不到固定原图:{SOURCE_IMAGE}")
|
||||
if not PROMPT_FILE.is_file():
|
||||
raise FileNotFoundError(f"找不到固定提示词文件:{PROMPT_FILE}")
|
||||
|
||||
client = OpenAI(
|
||||
api_key=api_key,
|
||||
base_url="https://xiaxiuxiu.com/v1",
|
||||
)
|
||||
prompt = PROMPT_FILE.read_text(encoding="utf-8").replace("{新标题}", TITLE)
|
||||
|
||||
with SOURCE_IMAGE.open("rb") as image_file:
|
||||
response = client.images.edit(
|
||||
model="gpt-image-2",
|
||||
image=image_file,
|
||||
prompt=prompt,
|
||||
size="1024x1024",
|
||||
n=1,
|
||||
)
|
||||
|
||||
item = response.data[0]
|
||||
if getattr(item, "b64_json", None):
|
||||
OUTPUT_IMAGE.write_bytes(base64.b64decode(item.b64_json))
|
||||
elif getattr(item, "url", None):
|
||||
urllib.request.urlretrieve(item.url, OUTPUT_IMAGE)
|
||||
else:
|
||||
raise RuntimeError(f"返回数据中没有图片内容:{item}")
|
||||
|
||||
print(f"已生成新图片并保存到本地:{OUTPUT_IMAGE}")
|
||||
Reference in New Issue
Block a user