mcpField
A
alicloud-ai-image-qwen-image

alicloud-ai-image-qwen-image

github.com/cinience创意与媒体396365 次安装11 周前更新
代码仓库

关于 alicloud-ai-image-qwen-image

此技能通过 DashScope SDK 调用 Qwen Image 模型,提供规范化的图像生成输入输出,适用于视频 Agent 流水线等图像生成工作流。请求可包含提示词、尺寸、种子和参考图像,并返回图像 URL 与尺寸信息。

主要功能

  1. 01调用指定的 Qwen Image 模型
  2. 02规范化图像生成请求与响应
  3. 03支持提示词、尺寸和参考图像
  4. 04返回图像 URL、尺寸和种子
  5. 05提供本地生成脚本

适用场景

  1. 01为视频 Agent 流水线生成图像
  2. 02使用种子复现图像生成请求

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/cinience/alicloud-skills --skill alicloud-ai-image-qwen-image

安装后在 Claude Code 中输入/alicloud-ai-image-qwen-image确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

agents/
openai.yaml
references/
api_reference.md
prompt-guide.md
sources.md
scripts/
generate_image.py

详细介绍

类别:provider

Model Studio Qwen Image

验证

mkdir -p output/alicloud-ai-image-qwen-image
python -m py_compile skills/ai/image/alicloud-ai-image-qwen-image/scripts/generate_image.py && echo "py_compile_ok" > output/alicloud-ai-image-qwen-image/validate.txt

通过标准:命令以 0 退出,且生成 output/alicloud-ai-image-qwen-image/validate.txt。

输出与证据

  • 将生成的图像 URL、提示词和元数据写入 output/alicloud-ai-image-qwen-image/。
  • 每次运行至少保留一个示例 JSON 响应。

通过标准化 image.generate 的输入/输出,并使用 DashScope SDK(Python)及准确的模型名称,为视频 Agent 工作流构建一致的图像生成行为。

前置条件

  • 安装 SDK(建议在 venv 中安装,以避免 PEP 668 限制):
python3 -m venv .venv
. .venv/bin/activate
python -m pip install dashscope
  • 在环境中设置 DASHSCOPE_API_KEY,或将 dashscope_api_key 添加到 ~/.alibabacloud/credentials(环境变量优先)。

关键模型名称

使用以下准确的模型字符串之一:

  • qwen-image
  • qwen-image-plus
  • qwen-image-max
  • qwen-image-2.0
  • qwen-image-2.0-pro
  • qwen-image-2.0-2026-03-03
  • qwen-image-2.0-pro-2026-03-03
  • qwen-image-max-2025-12-30
  • qwen-image-plus-2026-01-09

标准化接口(image.generate)

请求
  • prompt(字符串,必填)
  • negative_prompt(字符串,可选)
  • size(字符串,必填),例如 1024*1024、768*1024
  • style(字符串,可选)
  • seed(整数,可选)
  • reference_image(字符串 | 字节,可选)
响应
  • image_url(字符串)
  • width(整数)
  • height(整数)
  • seed(整数)

快速入门(标准化请求 + 预览)

最简标准化请求正文:

{
  "prompt": "a cinematic portrait of a cyclist at dusk, soft rim light, shallow depth of field",
  "negative_prompt": "blurry, low quality, watermark",
  "size": "1024*1024",
  "seed": 1234
}

预览工作流(下载后打开):

curl -L -o output/alicloud-ai-image-qwen-image/images/preview.png "<IMAGE_URL_FROM_RESPONSE>" && open output/alicloud-ai-image-qwen-image/images/preview.png

本地辅助脚本(JSON 请求 -> 图像文件):

python skills/ai/image/alicloud-ai-image-qwen-image/scripts/generate_image.py \\
  --request '{"prompt":"a studio product photo of headphones","size":"1024*1024"}' \\
  --output output/alicloud-ai-image-qwen-image/images/headphones.png \\
  --print-response

参数一览

字段必填说明
prompt是描述一个场景,而不只是关键词。
negative_prompt否尽力处理,后端可能会忽略。
size是WxH 格式,例如 1024*1024、768*1024。
style否可选的风格提示。
seed否在支持时用于确保可复现性。
reference_image否URL/文件/字节,具体映射取决于 SDK。

快速入门(Python + DashScope SDK)

使用 DashScope SDK,并将标准化请求映射到 SDK 调用。 注意:对于 qwen-image-max,当前通过基于 messages 的 ImageGeneration 调用比通过 ImageSynthesis 调用更容易成功。 如果你使用的 SDK 版本对参考图像要求使用不同的字段名,请相应调整 input 映射。

import os
from dashscope.aigc.image_generation import ImageGeneration

# Prefer env var for auth: export DASHSCOPE_API_KEY=...
# Or use ~/.alibabacloud/credentials with dashscope_api_key under [default].


def generate_image(req: dict) -> dict:
    messages = [
        {
            "role": "user",
            "content": [{"text": req["prompt"]}],
        }
    ]

    if req.get("reference_image"):
        # Some SDK versions accept {"image": <url|file|bytes>} in messages content.
        messages[0]["content"].insert(0, {"image": req["reference_image"]})

    response = ImageGeneration.call(
        model=req.get("model", "qwen-image-max"),
        messages=messages,
        size=req.get("size", "1024*1024"),
        api_key=os.getenv("DASHSCOPE_API_KEY"),
        # Pass through optional parameters if supported by the backend.
        negative_prompt=req.get("negative_prompt"),
        style=req.get("style"),
        seed=req.get("seed"),
    )

    # Response is a generation-style envelope; extract the first image URL.
    content = response.output["choices"][0]["message"]["content"]
    image_url = None
    for item in content:
        if isinstance(item, dict) and item.get("image"):
            image_url = item["image"]
            break
    return {
        "image_url": image_url,
        "width": response.usage.get("width"),
        "height": response.usage.get("height"),
        "seed": req.get("seed"),
    }

错误处理

错误可能原因操作
401/403缺少或无效的 DASHSCOPE_API_KEY检查环境变量或 ~/.alibabacloud/credentials,以及访问策略。
400不支持的尺寸或请求格式错误使用常见的 WxH 格式并验证字段。
429速率限制或配额限制退避后重试,或降低并发量。
5xx暂时性后端错误退避后重试一到两次。

输出位置

  • 默认输出:output/alicloud-ai-image-qwen-image/images/
  • 使用 OUTPUT_DIR 覆盖基础目录。

操作指南

  • 将返回的图像存储在对象存储中,并且只在元数据中持久化 URL。
  • 按 (prompt, negative_prompt, size, seed, reference_image hash) 缓存结果,以避免重复计费。
  • 对暂时性的 429/5xx 响应使用指数退避重试。
  • 某些后端会忽略 negative_prompt、style 或 seed;应将它们视为尽力处理的输入。
  • 如果响应中不包含图像 URL,应明确报错,并使用简化后的提示词重试一次。

尺寸说明

  • 使用 WxH 格式(例如 1024*1024、768*1024)。
  • 优先使用常见尺寸;不支持的尺寸可能会返回 400。

反模式

  • 不要臆造模型名称或别名;只能使用官方模型 ID。
  • 不要将大型 base64 数据块存储在数据库行中;请使用对象存储。
  • 长时间生成时,不要省略面向用户的进度信息。

工作流

  1. 确认用户意图、区域、标识符,以及操作是只读还是会修改数据。
  2. 先运行一条最简只读查询,以验证连接和权限。
  3. 使用明确的参数和有限的范围执行目标操作。
  4. 验证结果并保存输出/证据文件。

参考资料

  • 有关更详细的 DashScope SDK 映射和响应解析提示,请参阅 references/api_reference.md。

  • 有关提示词模式和示例,请参阅 references/prompt-guide.md。

  • 对于编辑工作流,请使用 skills/ai/image/alicloud-ai-image-qwen-image-edit/。

  • 来源列表:references/sources.md

评论

支持 Markdown