跳转到正文

图片生成与编辑 ​

输入一段描述,让模型生成图片。想修改已有图片,可以使用本页后面的参考图编辑示例。

先准备平台 API Key,并从模型广场选择支持图片生成的模型。

先生成一张图片 ​

完整请求地址:

text
https://www.taokennet.com/v1/images/generations

把示例中的 YOUR_IMAGE_MODEL_ID 换成实际图片模型名称。先使用默认尺寸跑通;之后再按模型说明添加尺寸、数量等选项。

Python 示例 ​

先安装 requests,方法见 Python 准备教程。保存下面代码为 image_demo.py:

python
import base64
import getpass
from pathlib import Path
import requests

API_BASE_URL = "https://www.taokennet.com"
MODEL_ID = "YOUR_IMAGE_MODEL_ID"
api_key = getpass.getpass("粘贴你的 API Key,然后按回车:").strip()

response = requests.post(
    f"{API_BASE_URL}/v1/images/generations",
    headers={"Authorization": f"Bearer {api_key}"},
    json={
        "model": MODEL_ID,
        "prompt": "一只坐在窗边的小猫,柔和的午后光线,插画风格",
    },
    timeout=600,
)

if not response.ok:
    print("请求失败:", response.status_code, response.text)
    raise SystemExit(1)

image = response.json()["data"][0]
if image.get("b64_json"):
    data = base64.b64decode(image["b64_json"])
    if data.startswith(b"\xff\xd8"):
        suffix = ".jpg"
    elif data[8:12] == b"WEBP":
        suffix = ".webp"
    else:
        suffix = ".png"
    filename = "result" + suffix
    Path(filename).write_bytes(data)
    print("图片已保存到:", Path(filename).resolve())
elif image.get("url"):
    print("在浏览器打开这个链接,再保存图片:")
    print(image["url"])
else:
    print("没有找到图片内容,请保留这次返回结果联系平台支持。")

运行:

bash
python image_demo.py

按提示填写 API Key,等待生成:

  • 如果返回图片文件内容,示例会保存为 result.png、result.jpg 或 result.webp。
  • 如果返回图片链接,复制链接到浏览器打开,再保存图片。

文件默认保存在你运行命令的文件夹,终端也会显示保存位置。

cURL 示例 ​

以下使用 Bash。替换模型名称后整段运行:

bash
API_BASE_URL='https://www.taokennet.com'
printf '粘贴你的 API Key,然后按回车:'
read -r -s API_KEY
printf '\n'

curl -sS "$API_BASE_URL/v1/images/generations" \
  -H "Authorization: Bearer $API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "YOUR_IMAGE_MODEL_ID",
    "prompt": "一只坐在窗边的小猫,柔和的午后光线,插画风格"
  }' \
  -o image-result.json

cat image-result.json

结果会保存到 image-result.json。如果其中有 data → url,复制该链接到浏览器查看。若只有很长的 b64_json 字符串,可以改用 Python 或 PowerShell 示例自动保存图片。

PowerShell 示例 ​

打开 Windows PowerShell,替换模型名称后运行:

powershell
$ErrorActionPreference = 'Stop'
$apiBaseUrl = 'https://www.taokennet.com'
$apiKey = Read-Host '粘贴你的 API Key,然后按回车'
$headers = @{ Authorization = "Bearer $($apiKey.Trim())" }
$body = @{
  model = 'YOUR_IMAGE_MODEL_ID'
  prompt = '一只坐在窗边的小猫,柔和的午后光线,插画风格'
} | ConvertTo-Json -Depth 5

$result = Invoke-RestMethod `
  -Uri "$apiBaseUrl/v1/images/generations" `
  -Method Post `
  -Headers $headers `
  -ContentType 'application/json' `
  -Body ([Text.Encoding]::UTF8.GetBytes($body)) `
  -TimeoutSec 600

$image = $result.data[0]
if ($image.b64_json) {
  $bytes = [Convert]::FromBase64String($image.b64_json)
  $extension = if ($bytes.Length -ge 2 -and $bytes[0] -eq 255 -and $bytes[1] -eq 216) {
    '.jpg'
  } elseif ($bytes.Length -ge 12 -and [Text.Encoding]::ASCII.GetString($bytes, 8, 4) -eq 'WEBP') {
    '.webp'
  } else {
    '.png'
  }
  $file = Join-Path (Get-Location) "result$extension"
  [IO.File]::WriteAllBytes($file, $bytes)
  Write-Host "图片已保存到:$file"
} elseif ($image.url) {
  Write-Host '在浏览器打开这个链接,再保存图片:'
  Write-Host $image.url
} else {
  Write-Host '没有找到图片内容,请保留这次返回结果联系平台支持。'
}

生成成功后,按终端提示打开文件或图片链接。图片链接可能会过期,满意的结果请及时保存。

用参考图修改图片 ​

先在模型广场确认模型支持“图片编辑”。生成图片和编辑图片是两项能力,请分别确认。

下面提供一个 Python 示例:把要修改的图片放到示例文件旁边,命名为 reference.png,将代码保存为 image_edit.py,替换模型名称后运行。

python
import getpass
import base64
from pathlib import Path
import requests

API_BASE_URL = "https://www.taokennet.com"
MODEL_ID = "YOUR_IMAGE_MODEL_ID"
api_key = getpass.getpass("粘贴你的 API Key,然后按回车:").strip()

with open("reference.png", "rb") as image_file:
    response = requests.post(
        f"{API_BASE_URL}/v1/images/edits",
        headers={"Authorization": f"Bearer {api_key}"},
        data={
            "model": MODEL_ID,
            "prompt": "保留主体,把背景改成海边日落",
        },
        files={"image": ("reference.png", image_file, "image/png")},
        timeout=600,
    )

if not response.ok:
    print("请求失败:", response.status_code, response.text)
    raise SystemExit(1)

image = response.json()["data"][0]
if image.get("b64_json"):
    data = base64.b64decode(image["b64_json"])
    if data.startswith(b"\xff\xd8"):
        suffix = ".jpg"
    elif data[8:12] == b"WEBP":
        suffix = ".webp"
    else:
        suffix = ".png"
    filename = "edited-result" + suffix
    Path(filename).write_bytes(data)
    print("图片已保存到:", Path(filename).resolve())
elif image.get("url"):
    print("在浏览器打开这个链接,再保存修改后的图片:")
    print(image["url"])
else:
    print("没有找到图片内容,请保留这次返回结果联系平台支持。")

模型对文件类型、大小、参考图数量和编辑方式可能有不同要求,以该模型说明为准。先从一张 PNG 参考图开始,不要一次添加大量图片。

常见问题 ​

问题先这样处理
提示模型不支持图片换成模型广场中支持图片的模型
提示尺寸或参数无效先删去额外尺寸、数量选项,只保留本页基本字段
一直没有结果或请求超时先查看使用记录,不要连续重复点击生成
结果链接打不开检查是否完整复制,及时保存,过期后联系平台支持
图片文件找不到查看程序输出的位置,或打开运行命令的文件夹

继续:视频生成 · 常见问题。