🔄 Synchronized updates: This project is released simultaneously on GitHub and ClawHub .
Quick Start
Prerequisites
# 配置 API Key
export DASHSCOPE_API_KEY="sk-xxxx"
Text-to-Image
import os, requests
resp = requests.post( "https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation", headers={"Authorization": f"Bearer {os.getenv('DASHSCOPE_API_KEY')}"}, json={ "model": "qwen-image-plus", "input": {"messages": [{"role": "user", "content": [{"text": "An orange cat dozing in the sunlight"}]}]}, "parameters": {"prompt_extend": True, "watermark": False} } ) print(resp.json()["output"]["choices"][0]["message"]["content"][0]["image"])
### 图像编辑
```python
resp = requests.post(
"https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation",
headers={"Authorization": f"Bearer {os.getenv('DASHSCOPE_API_KEY')}"},
json={
"model": "qwen-image-edit-plus",
"input": {"messages": [{"role": "user", "content": [
{"image": "https://example.com/photo.jpg"},
{"text": "把背景换成星空"}
]}]},
"parameters": {"prompt_extend": True, "watermark": False}
}
)
print(resp.json()["output"]["choices"][0]["message"]["content"][0]["image"])
Image Translation
import os, requests, time
# 1. 创建翻译任务
resp = requests.post(
"https://dashscope.aliyuncs.com/api/v1/services/aigc/image2image/image-synthesis",
headers={
"Authorization": f"Bearer {os.getenv('DASHSCOPE_API_KEY')}",
"X-DashScope-Async": "enable"
},
json={
"model": "qwen-mt-image",
"input": {
"image_url": "https://example.com/english-poster.jpg",
"source_lang": "en",
"target_lang": "zh"
}
}
)
task_id = resp.json()["output"]["task_id"]
# 2. 轮询获取结果
while True:
time.sleep(3)
result = requests.get(
f"https://dashscope.aliyuncs.com/api/v1/tasks/{task_id}",
headers={"Authorization": f"Bearer {os.getenv('DASHSCOPE_API_KEY')}"}
).json()
if result["output"]["task_status"] == "SUCCEEDED":
print(result["output"]["image_url"])
break
Default Configuration
| Scenario | Default Model | High-Quality Model |
|---|---|---|
| Text-to-Image | qwen-image-plus |
qwen-image-max |
| Image Editing | qwen-image-edit-plus |
qwen-image-edit-max |
| Image Translation | qwen-mt-image |
- |
Rule: Use the Plus series by default; use the Max series only when the user explicitly requests "the best" or "the highest quality".
Default Parameters
{
"negative_prompt": "低分辨率,低画质,肢体畸形,手指畸形,画面过饱和,蜡像感",
"prompt_extend": true,
"watermark": false
}
Detailed Reference
Load the following documents on demand:
| Document | Purpose |
|---|---|
| references/text-to-image.md | Complete API reference for text-to-image: model list, resolution, all parameters |
| references/image-edit.md | Complete API reference for image editing: single-image editing, multi-image fusion, input requirements |
| references/image-translate.md | Complete API reference for image translation: supported languages, asynchronous calls, terminology definitions |
Tool Scripts
| Script | Purpose |
|---|---|
| scripts/client.py | A pre-packaged API client that supports text-to-image, image editing, and image translation |
Using the Script:
# 文生图
python scripts/client.py generate "一只橘猫在阳光下打盹" --size 1920*1080
# 图像编辑
python scripts/client.py edit "https://example.com/photo.jpg" "把背景换成星空" -n 2
# 图像翻译
python scripts/client.py translate "https://example.com/english.jpg" --source en --target zh
Key Notes
- Image URL Validity Period: Generated image URLs are retained only for 24 hours, please download them promptly
- Region: The API Key and request URL must belong to the same region (Beijing/Singapore)
- Fees: Billing is based on the number of successfully generated images
Common Errors
| Error Code | Cause | Solution |
|---|---|---|
InvalidApiKey |
Invalid API Key | Check the environment variable configuration |
InvalidParameter |
Parameter error | Check the request format |
DataInspectionFailed |
Content review failed | Modify the input content |