feat(image): support arbitrary size and aspect ratio shortcuts for doubao generate

- --size now accepts free-form values (2K, 3K, 4K, 2304x1728, etc.)
  instead of a fixed [1K, 2K] choice.
- Add --ratio shortcut (1:1, 4:3, 3:4, 16:9, 9:16) mapping to pixel sizes.
- When both --size and --ratio are given, --size takes priority.
- Default remains 2K when neither is specified.
This commit is contained in:
Zhengshou Lai
2026-05-05 23:43:58 +08:00
parent e8ee5a4d0a
commit dea83774a5
+18 -3
View File
@@ -187,15 +187,25 @@ def compress_images(files, target_mb, max_width, quality, suffix, dry_run):
click.echo(f"Compressed: {img_file} -> {output} ({size_mb:.2f} MB)")
_RATIO_MAP = {
"1:1": "1024x1024",
"4:3": "1024x768",
"3:4": "768x1024",
"16:9": "1280x720",
"9:16": "720x1280",
}
@image.command("generate")
@click.argument("prompt", required=False)
@click.option("-f", "--file", type=click.Path(exists=True), help="从文件读取 prompt")
@click.option("-s", "--size", default="2K", type=click.Choice(["1K", "2K"]), help="图片尺寸")
@click.option("-s", "--size", help='图片尺寸 (如 2K, 3K, 4K 或 2304x1728)')
@click.option("-r", "--ratio", type=click.Choice(list(_RATIO_MAP.keys())), help="快捷比例 (1:1, 4:3, 3:4, 16:9, 9:16)")
@click.option("-o", "--output", help="保存路径(默认只输出 URL")
@click.option("--no-watermark", is_flag=True, help="不添加豆包水印")
@click.option("--b64", is_flag=True, help="使用 b64_json 格式获取图片数据")
@handle_errors
def generate_image_cmd(prompt, file, size, output, no_watermark, b64):
def generate_image_cmd(prompt, file, size, ratio, output, no_watermark, b64):
"""使用豆包/Seedream 模型生成图片。"""
from openai import OpenAI
@@ -209,6 +219,11 @@ def generate_image_cmd(prompt, file, size, output, no_watermark, b64):
click.echo("Error: apikey_ark not set. Run: mytoolkit env set apikey_ark <value>", err=True)
raise click.Abort()
if size and ratio:
click.echo("Warning: --size 和 --ratio 同时指定,--size 优先", err=True)
resolved_size = size or (ratio and _RATIO_MAP[ratio]) or "2K"
client = OpenAI(
base_url="https://ark.cn-beijing.volces.com/api/v3",
api_key=api_key,
@@ -217,7 +232,7 @@ def generate_image_cmd(prompt, file, size, output, no_watermark, b64):
resp = client.images.generate(
model="doubao-seedream-5-0-260128",
prompt=prompt,
size=size,
size=resolved_size,
response_format="b64_json" if b64 else "url",
extra_body={"watermark": not no_watermark},
)