使用 Model Studio DashScope SDK,调用通义万象图像生成模型(qwen-image、qwen-image-plus、qwen-image-max 及快照版)生成图像。适用于实现图像生成功能。
類別: 提供者 : 模擬工作室 Quen 影像.是一项面向实际任务的技能,主要用于Validation.;Pass 標準: 命令出口 0 和输出/ alicloud-ai-image- qwen-image/validate.txt 生成..;
使用时应结合输入条件选择合适的执行方式,核对必要参数、依赖环境与输出内容,并按原始要求处理异常情况。从功能定位来看,该技能强调把分散的操作要求整理成清晰、可复用的处理流程,使用户能够围绕既定目标快速准备输入、选择执行方式并获得结构化结果。实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。
执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。该技能适合用于一次性任务,也可以接入自动化工作流,与其他技能或上层代理配合完成更完整的业务链路;在组合使用时,应明确每一步的输入输出关系,并避免不同步骤之间出现参数冲突。
类别:provider
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 文件。
output/alicloud-ai-image-qwen-image/ 目录。通过对 image.generate 的输入/输出进行标准化,并严格使用 DashScope SDK(Python)及精确的模型名称,为 video-agent 流水线构建一致的图像生成行为。
python3 -m venv .venv
. .venv/bin/activate
python -m pip install dashscope
DASHSCOPE_API_KEY,或在 ~/.alibabacloud/credentials 中添加 dashscope_api_key 字段(环境变量优先级更高)。必须使用以下任一精确的模型字符串:
qwen-imageqwen-image-plusqwen-image-maxqwen-image-2.0qwen-image-2.0-proqwen-image-max-2025-12-30qwen-image-plus-2026-01-09prompt(字符串,必填)negative_prompt(字符串,可选)size(字符串,必填),例如 1024*1024、768*1024style(字符串,可选)seed(整数,可选)reference_image(字符串或 bytes,可选)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 "" && 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 / 本地文件路径 / bytes;具体映射方式取决于 SDK 实现。 |
使用 DashScope SDK,并将标准化请求映射至 SDK 调用。
注意:对于 qwen-image-max,当前 DashScope SDK 通过基于 messages 的 ImageGeneration 接口调用成功,而非 ImageSynthesis。
若所用 SDK 版本对参考图像字段使用了不同命名,请相应调整 input 映射逻辑。
import os
from dashscope.aigc.image_generation import ImageGeneration
# 推荐使用环境变量认证:export DASHSCOPE_API_KEY=...
# 或在 ~/.alibabacloud/credentials 中配置 dashscope_api_key(位于 [default] 段下)。
def generate_image(req: dict) -> dict:
messages = [
{
"role": "user",
"content": [{"text": req["prompt"]}],
}
]
if req.get("reference_image"):
# 部分 SDK 版本支持在 messages content 中传入 {"image": }。
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"),
# 若后端支持,透传可选参数。
negative_prompt=req.get("negative_prompt"),
style=req.get("style"),
seed=req.get("seed"),
)
# 响应为 generation 风格封装体;提取首个图像 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 覆盖基础路径。(prompt, negative_prompt, size, seed, reference_image hash) 缓存结果,避免重复调用开销。negative_prompt、style 或 seed,应将其视为尽力而为(best-effort)输入。image_url,需明确报错,并使用简化 prompt 重试一次。WxH 格式(例如 1024*1024、768*1024)。详见 references/api_reference.md,获取更详细的 DashScope SDK 映射说明与响应解析技巧。
详见 references/prompt-guide.md,了解提示词设计模式与示例。
如需编辑类工作流,请使用 skills/ai/image/alicloud-ai-image-qwen-image-edit/。
源信息列表:references/sources.md