alicloud-ai-image-qwen-image
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCategory: provider
分类:provider
Model Studio Qwen Image
Model Studio Qwen图像生成
Build consistent image generation behavior for the video-agent pipeline by standardizing inputs/outputs and using DashScope SDK (Python) with the exact model name.
image.generate通过标准化的输入/输出,并结合指定模型名称使用DashScope SDK(Python),为video-agent工作流构建一致的图像生成行为。
image.generatePrerequisites
前提条件
- Install SDK (recommended in a venv to avoid PEP 668 limits):
bash
python3 -m venv .venv
. .venv/bin/activate
python -m pip install dashscope- Set in your environment, or add
DASHSCOPE_API_KEYtodashscope_api_key(env takes precedence).~/.alibabacloud/credentials
- 安装SDK(建议在虚拟环境中安装,以避免PEP 668限制):
bash
python3 -m venv .venv
. .venv/bin/activate
python -m pip install dashscope- 在环境变量中设置,或在
DASHSCOPE_API_KEY中添加~/.alibabacloud/credentials(环境变量优先级更高)。dashscope_api_key
Critical model names
关键模型名称
Use one of these exact model strings:
qwen-image-maxqwen-image-plus-2026-01-09
请使用以下精确的模型字符串之一:
qwen-image-maxqwen-image-plus-2026-01-09
Normalized interface (image.generate)
标准化接口(image.generate)
Request
请求参数
- (string, required)
prompt - (string, optional)
negative_prompt - (string, required) e.g.
size,1024*1024768*1024 - (string, optional)
style - (int, optional)
seed - (string | bytes, optional)
reference_image
- (字符串,必填)
prompt - (字符串,可选)
negative_prompt - (字符串,必填)示例:
size、1024*1024768*1024 - (字符串,可选)
style - (整数,可选)
seed - (字符串 | 字节,可选)
reference_image
Response
响应参数
- (string)
image_url - (int)
width - (int)
height - (int)
seed
- (字符串)
image_url - (整数)
width - (整数)
height - (整数)
seed
Quickstart (normalized request + preview)
快速入门(标准化请求 + 预览)
Minimal normalized request body:
json
{
"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
}Preview workflow (download then open):
bash
curl -L -o output/ai-image-qwen-image/images/preview.png "<IMAGE_URL_FROM_RESPONSE>" && open output/ai-image-qwen-image/images/preview.pngLocal helper script (JSON request -> image file):
bash
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/ai-image-qwen-image/images/headphones.png \\
--print-response最简标准化请求体:
json
{
"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
}预览流程(下载后打开):
bash
curl -L -o output/ai-image-qwen-image/images/preview.png "<IMAGE_URL_FROM_RESPONSE>" && open output/ai-image-qwen-image/images/preview.png本地辅助脚本(JSON请求 -> 图像文件):
bash
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/ai-image-qwen-image/images/headphones.png \\
--print-responseParameters at a glance
参数一览
| Field | Required | Notes |
|---|---|---|
| yes | Describe a scene, not just keywords. |
| no | Best-effort, may be ignored by backend. |
| yes | |
| no | Optional stylistic hint. |
| no | Use for reproducibility when supported. |
| no | URL/file/bytes, SDK-specific mapping. |
| 字段 | 必填 | 说明 |
|---|---|---|
| 是 | 描述场景,而非仅使用关键词。 |
| 否 | 尽力生效,可能被后端忽略。 |
| 是 | |
| 否 | 可选的风格提示。 |
| 否 | 支持时用于生成结果的可复现性。 |
| 否 | URL/文件/字节,SDK特定映射方式。 |
Quick start (Python + DashScope SDK)
快速入门(Python + DashScope SDK)
Use the DashScope SDK and map the normalized request into the SDK call.
Note: For , the DashScope SDK currently succeeds via (messages-based) rather than .
If the SDK version you are using expects a different field name for reference images, adapt the mapping accordingly.
qwen-image-maxImageGenerationImageSynthesisinputpython
import os
from dashscope.aigc.image_generation import ImageGeneration使用DashScope SDK,并将标准化请求映射到SDK调用中。
注意:对于,当前DashScope SDK需通过(基于消息的方式)而非调用才能成功。
如果您使用的SDK版本对参考图像的字段名称要求不同,请相应调整映射。
qwen-image-maxImageGenerationImageSynthesisinputpython
import os
from dashscope.aigc.image_generation import ImageGenerationPrefer env var for auth: export DASHSCOPE_API_KEY=...
优先使用环境变量认证:export DASHSCOPE_API_KEY=...
Or use ~/.alibabacloud/credentials with dashscope_api_key under [default].
或在~/.alibabacloud/credentials的[default]下配置dashscope_api_key。
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"),
}undefineddef generate_image(req: dict) -> dict:
messages = [
{
"role": "user",
"content": [{"text": req["prompt"]}],
}
]
if req.get("reference_image"):
# 部分SDK版本接受在messages content中传入{"image": <url|file|bytes>}。
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"),
)
# 响应为生成式信封结构;提取第一个图像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"),
}undefinedError handling
错误处理
| Error | Likely cause | Action |
|---|---|---|
| 401/403 | Missing or invalid | Check env var or |
| 400 | Unsupported size or bad request shape | Use common |
| 429 | Rate limit or quota | Retry with backoff, or reduce concurrency. |
| 5xx | Transient backend errors | Retry with backoff once or twice. |
| 错误 | 可能原因 | 处理方式 |
|---|---|---|
| 401/403 | | 检查环境变量或 |
| 400 | 不支持的尺寸或请求格式错误 | 使用常见的 |
| 429 | 速率限制或配额不足 | 退避重试,或降低并发量。 |
| 5xx | 后端临时错误 | 退避重试1-2次。 |
Output location
输出位置
- Default output:
output/ai-image-qwen-image/images/ - Override base dir with .
OUTPUT_DIR
- 默认输出路径:
output/ai-image-qwen-image/images/ - 可通过覆盖基础目录。
OUTPUT_DIR
Operational guidance
操作指南
- Store the returned image in object storage and persist only the URL in metadata.
- Cache results by to avoid duplicate costs.
(prompt, negative_prompt, size, seed, reference_image hash) - Add retries for transient 429/5xx responses with exponential backoff.
- Some backends ignore ,
negative_prompt, orstyle; treat them as best-effort inputs.seed - If the response contains no image URL, surface a clear error and retry once with a simplified prompt.
- 将返回的图像存储在对象存储中,仅在元数据中保留URL。
- 通过缓存结果,避免重复成本。
(prompt, negative_prompt, size, seed, reference_image哈希) - 对临时的429/5xx响应添加指数退避重试机制。
- 部分后端会忽略、
negative_prompt或style;将它们视为尽力生效的输入。seed - 如果响应中不包含图像URL,需明确抛出错误并使用简化的prompt重试一次。
Size notes
尺寸说明
- Use format (e.g.
WxH,1024*1024).768*1024 - Prefer common sizes; unsupported sizes can return 400.
- 使用格式(例如
WxH、1024*1024)。768*1024 - 优先使用常见尺寸;不支持的尺寸会返回400错误。
Anti-patterns
反模式
- Do not invent model names or aliases; use official model IDs only.
- Do not store large base64 blobs in DB rows; use object storage.
- Do not omit user-visible progress for long generations.
- 不要自行创建模型名称或别名;仅使用官方模型ID。
- 不要在数据库行中存储大型base64 blob;使用对象存储。
- 长时生成过程中不要隐藏用户可见的进度提示。
References
参考资料
-
Seefor a more detailed DashScope SDK mapping and response parsing tips.
references/api_reference.md -
Seefor prompt patterns and examples.
references/prompt-guide.md -
For edit workflows, use.
skills/ai/image/alicloud-ai-image-qwen-image-edit/ -
Source list:
references/sources.md
-
详见获取更详细的DashScope SDK映射和响应解析技巧。
references/api_reference.md -
详见获取prompt模板和示例。
references/prompt-guide.md -
如需编辑工作流,请使用。
skills/ai/image/alicloud-ai-image-qwen-image-edit/ -
来源列表:
references/sources.md