$npx -y skills add cinience/alicloud-skills --skill aliyun-qwen-imageUse when generating images with Model Studio DashScope SDK using Qwen Image generation models (qwen-image, qwen-image-plus, qwen-image-max, qwen-image-2.0 series and snapshots). Use when implementing or documenting image.generate requests/responses, mapping prompt/negative_prompt
| 1 | Category: provider |
| 2 | |
| 3 | # Model Studio Qwen Image |
| 4 | |
| 5 | ## Validation |
| 6 | |
| 7 | ```bash |
| 8 | mkdir -p output/aliyun-qwen-image |
| 9 | python -m py_compile skills/ai/image/aliyun-qwen-image/scripts/generate_image.py && echo "py_compile_ok" > output/aliyun-qwen-image/validate.txt |
| 10 | ``` |
| 11 | |
| 12 | Pass criteria: command exits 0 and `output/aliyun-qwen-image/validate.txt` is generated. |
| 13 | |
| 14 | ## Output And Evidence |
| 15 | |
| 16 | - Write generated image URLs, prompts, and metadata to `output/aliyun-qwen-image/`. |
| 17 | - Keep at least one sample JSON response per run. |
| 18 | |
| 19 | Build consistent image generation behavior for the video-agent pipeline by standardizing `image.generate` inputs/outputs and using DashScope SDK (Python) with the exact model name. |
| 20 | |
| 21 | ## Prerequisites |
| 22 | |
| 23 | - Install SDK (recommended in a venv to avoid PEP 668 limits): |
| 24 | |
| 25 | ```bash |
| 26 | python3 -m venv .venv |
| 27 | . .venv/bin/activate |
| 28 | python -m pip install dashscope |
| 29 | ``` |
| 30 | - Set `DASHSCOPE_API_KEY` in your environment, or add `dashscope_api_key` to `~/.alibabacloud/credentials` (env takes precedence). |
| 31 | |
| 32 | ## Critical model names |
| 33 | |
| 34 | Use one of these exact model strings: |
| 35 | - `qwen-image` |
| 36 | - `qwen-image-plus` |
| 37 | - `qwen-image-max` |
| 38 | - `qwen-image-2.0` |
| 39 | - `qwen-image-2.0-pro` |
| 40 | - `qwen-image-2.0-2026-03-03` |
| 41 | - `qwen-image-2.0-pro-2026-03-03` |
| 42 | - `qwen-image-max-2025-12-30` |
| 43 | - `qwen-image-plus-2026-01-09` |
| 44 | |
| 45 | ## Normalized interface (image.generate) |
| 46 | |
| 47 | ### Request |
| 48 | - `prompt` (string, required) |
| 49 | - `negative_prompt` (string, optional) |
| 50 | - `size` (string, required) e.g. `1024*1024`, `768*1024` |
| 51 | - `style` (string, optional) |
| 52 | - `seed` (int, optional) |
| 53 | - `reference_image` (string | bytes, optional) |
| 54 | |
| 55 | ### Response |
| 56 | - `image_url` (string) |
| 57 | - `width` (int) |
| 58 | - `height` (int) |
| 59 | - `seed` (int) |
| 60 | |
| 61 | ## Quickstart (normalized request + preview) |
| 62 | |
| 63 | Minimal normalized request body: |
| 64 | |
| 65 | ```json |
| 66 | { |
| 67 | "prompt": "a cinematic portrait of a cyclist at dusk, soft rim light, shallow depth of field", |
| 68 | "negative_prompt": "blurry, low quality, watermark", |
| 69 | "size": "1024*1024", |
| 70 | "seed": 1234 |
| 71 | } |
| 72 | ``` |
| 73 | |
| 74 | Preview workflow (download then open): |
| 75 | |
| 76 | ```bash |
| 77 | curl -L -o output/aliyun-qwen-image/images/preview.png "<IMAGE_URL_FROM_RESPONSE>" && open output/aliyun-qwen-image/images/preview.png |
| 78 | ``` |
| 79 | |
| 80 | Local helper script (JSON request -> image file): |
| 81 | |
| 82 | ```bash |
| 83 | python skills/ai/image/aliyun-qwen-image/scripts/generate_image.py \\ |
| 84 | --request '{"prompt":"a studio product photo of headphones","size":"1024*1024"}' \\ |
| 85 | --output output/aliyun-qwen-image/images/headphones.png \\ |
| 86 | --print-response |
| 87 | ``` |
| 88 | |
| 89 | ## Parameters at a glance |
| 90 | |
| 91 | | Field | Required | Notes | |
| 92 | |------|----------|-------| |
| 93 | | `prompt` | yes | Describe a scene, not just keywords. | |
| 94 | | `negative_prompt` | no | Best-effort, may be ignored by backend. | |
| 95 | | `size` | yes | `WxH` format, e.g. `1024*1024`, `768*1024`. | |
| 96 | | `style` | no | Optional stylistic hint. | |
| 97 | | `seed` | no | Use for reproducibility when supported. | |
| 98 | | `reference_image` | no | URL/file/bytes, SDK-specific mapping. | |
| 99 | |
| 100 | ## Quick start (Python + DashScope SDK) |
| 101 | |
| 102 | Use the DashScope SDK and map the normalized request into the SDK call. |
| 103 | Note: For `qwen-image-max`, the DashScope SDK currently succeeds via `ImageGeneration` (messages-based) rather than `ImageSynthesis`. |
| 104 | If the SDK version you are using expects a different field name for reference images, adapt the `input` mapping accordingly. |
| 105 | |
| 106 | ```python |
| 107 | import os |
| 108 | from dashscope.aigc.image_generation import ImageGeneration |
| 109 | |
| 110 | # Prefer env var for auth: export DASHSCOPE_API_KEY=... |
| 111 | # Or use ~/.alibabacloud/credentials with dashscope_api_key under [default]. |
| 112 | |
| 113 | |
| 114 | def generate_image(req: dict) -> dict: |
| 115 | messages = [ |
| 116 | { |
| 117 | "role": "user", |
| 118 | "content": [{"text": req["prompt"]}], |
| 119 | } |
| 120 | ] |
| 121 | |
| 122 | if req.get("reference_image"): |
| 123 | # Some SDK versions accept {"image": <url|file|bytes>} in messages content. |
| 124 | messages[0]["content"].insert(0, {"image": req["reference_image"]}) |
| 125 | |
| 126 | response = ImageGeneration.call( |
| 127 | model=req.get("model", "qwen-image-max"), |
| 128 | messages=messages, |
| 129 | size=req.get("size", "1024*1024"), |
| 130 | api_key=os.getenv("DASHSCOPE_API_KEY"), |
| 131 | # Pass through optional parameters if supported by the backend. |
| 132 | negative_prompt=req.get("negative_prompt"), |
| 133 | style=req.get("style"), |
| 134 | seed=req.get("seed"), |
| 135 | ) |
| 136 | |
| 137 | # Response is a generation-style envelope; extract the first image URL. |
| 138 | content = response.output["choices"][0]["message"]["content"] |
| 139 | image_url = None |
| 140 | for item in content: |
| 141 | if isinstance(item, dict) and item.get("image"): |
| 142 | image_url = item["image"] |
| 143 | break |
| 144 | return { |
| 145 | "image_url": image_url, |
| 146 | "width": resp |