通過文生圖API,您可以基於文本描述創造出全新的映像。阿里雲百鍊提供兩大系列模型:
通義千問(Qwen-Image): 擅長渲染複雜的中英文文本。
通義萬相(Wan系列): 用於產生寫實映像和攝影級視覺效果。
模型效果
通義千問(Qwen-image)
複雜文字
| 超長段落
| 複雜布局
|
海報製作
| 插畫設計
| 寫實攝影
|
通義萬相
人像寫真
| 寫實攝影
| 繪畫流派
|
文字產生
| 海報設計
| 組圖產生
|
支援的模型
模型選型
複雜文字渲染(如海報、對聯):首選
qwen-image-plus、wan2.5-t2i-preview。寫實情境和攝影風格(通用情境):可選通義萬相模型,如
wan2.5-t2i-preview、wan2.2-t2i-flash。需要自訂輸出映像解析度:推薦通義萬相模型,如
wan2.2-t2i-flash,支援 [512, 1440] 像素範圍內的任意寬高組合。通義千問Qwen-Image僅支援5種固定尺寸:1664*928(16:9)、928*1664(9:16)、1328*1328(1:1)、1472*1140(4:3)、1140*1472(3:4)。
如需使用圖生圖模型,請參見影像編輯-通義千問和影像編輯-通義萬相2.1。
快速開始
前提條件
在調用前,請擷取與配置 API Key,再配置API Key到環境變數(準備下線,併入配置 API Key)。如果通過DashScope SDK進行調用,還需要安裝SDK。
範例程式碼
調用方式說明:通義千問Qwen-Image支援同步和非同步呼叫,通義萬相僅支援非同步呼叫。
非同步呼叫:兩者API 相容,只需更改
model參數即可輕鬆切換。例如,使用通義萬相模型時,將 model 設定為 "wan2.2-t2i-flash"。同步調用:僅通義千問Qwen-Image支援,調用請參見通義千問 Qwen-Image API參考。
下文樣本均為非同步呼叫方式。範例程式碼以 qwen-image-plus 為例,但同樣適用於通義萬相模型。
SDK 在底層封裝了非同步處理邏輯,上層介面表現為同步調用(即單次請求並等待最終結果返回);而 curl 樣本則對應兩個獨立的非同步 API 介面:一個用於提交任務,另一個用於查詢結果。
Python
請求樣本
from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
from dashscope import ImageSynthesis
import os
import dashscope
# 以下為新加坡地區url,若使用北京地區的模型,需將url替換為:https://dashscope.aliyuncs.com/api/v1
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
prompt = "Healing-style hand-drawn poster featuring three puppies playing with a ball on lush green grass, adorned with decorative elements such as birds and stars. The main title “Come Play Ball!” is prominently displayed at the top in bold, blue cartoon font. Below it, the subtitle “Come [Show Off Your Skills]!” appears in green font. A speech bubble adds playful charm with the text: “Hehe, watch me amaze my little friends next!” At the bottom, supplementary text reads: “We get to play ball with our friends again!” The color palette centers on fresh greens and blues, accented with bright pink and yellow tones to highlight a cheerful, childlike atmosphere."
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
# 若沒有配置環境變數,請用百鍊API Key將下行替換為:api_key="sk-xxx"
api_key = os.getenv("DASHSCOPE_API_KEY")
print('----sync call, please wait a moment----')
rsp = ImageSynthesis.call(api_key=api_key,
model="qwen-image-plus",
prompt=prompt,
n=1,
size='1328*1328',
prompt_extend=True,
watermark=False)
print(f'response: {rsp}')
if rsp.status_code == HTTPStatus.OK:
# 在目前的目錄下儲存映像
for result in rsp.output.results:
file_name = PurePosixPath(unquote(urlparse(result.url).path)).parts[-1]
with open('./%s' % file_name, 'wb+') as f:
f.write(requests.get(result.url).content)
else:
print(f'sync_call Failed, status_code: {rsp.status_code}, code: {rsp.code}, message: {rsp.message}')
響應樣本
url 有效期間24小時,請及時下載映像。
{
"status_code": 200,
"request_id": "a47b1a65-7041-4565-9068-xxxxxx",
"code": null,
"message": "",
"output": {
"task_id": "91093132-475e-43cf-b94e-xxxxxx",
"task_status": "SUCCEEDED",
"results": [
{
"url": "https://dashscope-result-sz.oss-cn-shenzhen.aliyuncs.com/xxx.png?Expires=xxxxxx",
"orig_prompt": "Healing-style hand-drawn poster featuring three puppies playing with a ball on lush green grass, adorned with decorative elements such as birds and stars. The main title “Come Play Ball!” is prominently displayed at the top in bold, blue cartoon font. Below it, the subtitle “Come [Show Off Your Skills]!” appears in green font. A speech bubble adds playful charm with the text: “Hehe, watch me amaze my little friends next!” At the bottom, supplementary text reads: “We get to play ball with our friends again!” The color palette centers on fresh greens and blues, accented with bright pink and yellow tones to highlight a cheerful, childlike atmosphere.",
"actual_prompt": "Childhood-inspired hand-drawn poster design: Three playful puppies joyfully interact with a colorful ball on a vibrant patch of lush green grass. Delicate decorative elements including fluttering birds and twinkling stars are scattered throughout. At the top center, the bold, blue cartoon-style title “Come Play Ball!” stands out prominently. Directly beneath, the subtitle “Come [Show Off Your Skills]!” is rendered in cheerful green lettering. A whimsical speech bubble near one of the puppies contains the playful text: “Hehe, watch me amaze my little friends next!” At the bottom edge, smaller supplementary text reads: “We get to play ball with our friends again!” The color palette is centered on fresh greens and sky blues, accented with pops of bright pink and sunny yellow, enhancing the cheerful, childlike atmosphere. Style evokes nostalgic, hand-inked illustrations with soft textures, gentle linework, and a whimsical, storybook-like composition."
}
],
"submit_time": "2025-09-09 13:39:20.659",
"scheduled_time": "2025-09-09 13:39:20.717",
"end_time": "2025-09-09 13:39:45.233"
},
"usage": {
"image_count": 1
}
}Java
請求樣本
// Copyright (c) Alibaba, Inc. and its affiliates.
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisListResult;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisParam;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.task.AsyncTaskListParam;
import com.alibaba.dashscope.utils.Constants;
import com.alibaba.dashscope.utils.JsonUtils;
import java.util.HashMap;
import java.util.Map;
public class Text2Image {
static {
// 以下為新加坡地區url,若使用北京地區的模型,需將url替換為:https://dashscope.aliyuncs.com/api/v1
Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
}
// 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
// 若沒有配置環境變數,請用百鍊API Key將下行替換為:static String apiKey = "sk-xxx"
static String apiKey = System.getenv("DASHSCOPE_API_KEY");
public static void basicCall() throws ApiException, NoApiKeyException {
String prompt = "Healing-style hand-drawn poster featuring three puppies playing with a ball on lush green grass, adorned with decorative elements such as birds and stars. The main title “Come Play Ball!” is prominently displayed at the top in bold, blue cartoon font. Below it, the subtitle “Come [Show Off Your Skills]!” appears in green font. A speech bubble adds playful charm with the text: “Hehe, watch me amaze my little friends next!” At the bottom, supplementary text reads: “We get to play ball with our friends again!” The color palette centers on fresh greens and blues, accented with bright pink and yellow tones to highlight a cheerful, childlike atmosphere.";
Map<String, Object> parameters = new HashMap<>();
parameters.put("prompt_extend", true);
parameters.put("watermark", false);
ImageSynthesisParam param =
ImageSynthesisParam.builder()
.apiKey(apiKey)
.model("qwen-image-plus")
.prompt(prompt)
.n(1)
.size("1328*1328")
.parameters(parameters)
.build();
ImageSynthesis imageSynthesis = new ImageSynthesis();
ImageSynthesisResult result = null;
try {
System.out.println("---sync call, please wait a moment----");
result = imageSynthesis.call(param);
} catch (ApiException | NoApiKeyException e){
throw new RuntimeException(e.getMessage());
}
System.out.println(JsonUtils.toJson(result));
}
public static void main(String[] args){
try{
basicCall();
}catch(ApiException|NoApiKeyException e){
System.out.println(e.getMessage());
}
}
}
響應樣本
url 有效期間24小時,請及時下載映像。
{
"request_id": "9f3044ba-528f-4606-8830-xxxxxx",
"output": {
"task_id": "fecf4c7f-3508-45f4-8454-xxxxxx",
"task_status": "SUCCEEDED",
"results": [
{
"orig_prompt": "Healing-style hand-drawn poster featuring three puppies playing with a ball on lush green grass, adorned with decorative elements such as birds and stars. The main title “Come Play Ball!” is prominently displayed at the top in bold, blue cartoon font. Below it, the subtitle “Come [Show Off Your Skills]!” appears in green font. A speech bubble adds playful charm with the text: “Hehe, watch me amaze my little friends next!” At the bottom, supplementary text reads: “We get to play ball with our friends again!” The color palette centers on fresh greens and blues, accented with bright pink and yellow tones to highlight a cheerful, childlike atmosphere.",
"actual_prompt": "Childhood-inspired hand-drawn poster design: Three playful puppies joyfully interact with a colorful ball on a vibrant patch of lush green grass. Delicate decorative elements including fluttering birds and twinkling stars are scattered throughout. At the top center, the bold, blue cartoon-style title “Come Play Ball!” stands out prominently. Directly beneath, the subtitle “Come [Show Off Your Skills]!” is rendered in cheerful green lettering. A whimsical speech bubble near one of the puppies contains the playful text: “Hehe, watch me amaze my little friends next!” At the bottom edge, smaller supplementary text reads: “We get to play ball with our friends again!” The color palette is centered on fresh greens and sky blues, accented with pops of bright pink and sunny yellow, enhancing the cheerful, childlike atmosphere. Style evokes nostalgic, hand-inked illustrations with soft textures, gentle linework, and a whimsical, storybook-like composition.",
"url": "https://dashscope-result-sz.oss-cn-shenzhen.aliyuncs.com/xxx.png?Expires=xxxx"
}
]
},
"usage": {
"image_count": 1
}
}curl
非同步呼叫必須設定 Header 參數
X-DashScope-Async為enable。非同步任務的
task_id查詢有效期間為 24 小時,到期後任務狀態將變為UNKNOWN。
步驟1:發起建立工作要求
該請求會返回一個任務ID(task_id)。
curl -X POST https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis \
-H 'X-DashScope-Async: enable' \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "qwen-image-plus",
"input": {
"prompt": "Healing-style hand-drawn poster featuring three puppies playing with a ball on lush green grass, adorned with decorative elements such as birds and stars. The main title “Come Play Ball!” is prominently displayed at the top in bold, blue cartoon font. Below it, the subtitle “Come [Show Off Your Skills]!” appears in green font. A speech bubble adds playful charm with the text: “Hehe, watch me amaze my little friends next!” At the bottom, supplementary text reads: “We get to play ball with our friends again!” The color palette centers on fresh greens and blues, accented with bright pink and yellow tones to highlight a cheerful, childlike atmosphere."
},
"parameters": {
"size": "1328*1328",
"n": 1,
"prompt_extend": true,
"watermark": false
}
}' 步驟2:根據任務ID查詢結果
使用上一步擷取的 task_id,通過介面輪詢任務狀態,直到 task_status 變為 SUCCEEDED 或 FAILED。
請將86ecf553-d340-4e21-xxxxxxxxx替換為真實的task_id。
新加坡和北京地區的API Key不同。擷取與配置 API Key。
以下為新加坡地區base_url,若使用北京地區的模型,需將base_url替換為https://dashscope.aliyuncs.com/api/v1/tasks/86ecf553-d340-4e21-xxxxxxxxx
curl -X GET https://dashscope-intl.aliyuncs.com/api/v1/tasks/86ecf553-d340-4e21-xxxxxxxxx \
--header "Authorization: Bearer $DASHSCOPE_API_KEY"關鍵能力
1. 指令遵循(提示詞)
參數:input.prompt(必選)、input.negative_prompt(可選)。
prompt(正向提示詞):描述希望在畫面中看到的內容、主體、情境、風格、光照和構圖。文生圖的核心控制參數。
negative_prompt(反向提示詞):描述不希望在畫面中出現的內容,如“模糊”、“多餘的手指”等。僅用於輔助最佳化產生品質。
撰寫技巧:一個結構化的 Prompt 通常能帶來更好的效果,撰寫技巧請參見文生圖Prompt指南。
2. 開啟prompt智能改寫
參數: parameters.prompt_extend (bool, 預設為 true)。
此功能可自動擴充和最佳化較短的Prompt,提升出圖效果。開啟此功能額外耗時 3-5 秒。此耗時為使用大模型改寫文本。
實踐建議:
建議開啟:當輸入 Prompt 較簡潔或寬泛時,此功能可顯著增益圖像效果。
建議關閉:若需控制畫面細節、或已提供詳細描述,或對響應延遲敏感。請將參數
prompt_extend顯式設為false。
3. 設定輸出映像解析度
參數: parameters.size (string),格式為 "寬*高"。
通義千問 Qwen-Image:僅支援以下 5 種固定的解析度:
1328*1328(預設值):1:1。
1664*928: 16:9。
928*1664: 9:16。
1472*1140: 4:3。
1140*1472: 3:4。
通義萬相 V2 版模型 (2.0 及以上版本):支援在 [512, 1440] 像素範圍內任意組合寬高,總像素不超過 1440*1440。常用解析度:
1024*1024(預設值):1:1。
1440*810: 16:9。
810*1440: 9:16。
1440*1080: 4:3。
1080*1440: 3:4。
應用於生產環境
容錯策略
處理限流:當 API 返回
Throttling錯誤碼或 HTTP 429 狀態代碼時,表明已觸發限流,限流處理請參見限流。非同步任務輪詢:輪詢查詢非同步任務結果時,建議採用合理的輪詢策略(如前30秒每3秒一次,之後拉長間隔),避免因過於頻繁的請求而觸發限流。為任務設定一個最終逾時時間(如 2 分鐘),逾時後標記為失敗。
風險防範
結果持久化:API 返回的圖片 URL 有 24 小時有效期間。生產系統必須在擷取 URL 後立即下載圖片,並轉存至您自己的持久化儲存服務中(如阿里雲Object Storage Service)。
Alibaba Content Security Service審核:所有
prompt和negative_prompt都會經過Alibaba Content Security Service審核。若輸入內容不合規,請求將被攔截並返回DataInspectionFailed錯誤。產生內容的著作權與合規風險:請確保您的提示詞內容符合相關法律法規。產生包含品牌商標、名人肖像、受著作權保護的 IP 形象等內容可能涉及侵權風險,請您自行評估並承擔相應責任。
API文檔
計費與限流
模型免費額度和計費單價請參見映像產生。
計費規則
計費項目:按成功產生的 映像張數 計費,採用按量後付費模式。
計費公式:費用 = 計費單價 × 映像張數。
抵扣順序:優先消耗免費額度。額度用盡後,預設轉為隨用隨付。
您可開啟“免費額度用完即停”功能,以避免免費額度耗盡後產生額外費用。詳情請參見免費額度。
失敗不計費:模型調用失敗或處理錯誤不產生任何費用,也不消耗免費額度。
免費額度
關於免費額度的領取、查詢、使用方法等詳情,請參見免費額度。
調用量查詢
模型調用完約一小時後,請在模型觀測(新加坡)頁面,查看調用量、調用次數、成功率等指標。
如果使用“華北2(北京)”地區的模型,請前往“華北2(北京)”地區的模型觀測頁面。
限流
模型限流規則及常見問題,請參見限流。
錯誤碼
如果模型調用失敗並返回報錯資訊,請參見錯誤資訊進行解決。
常見問題
Q: 圖片 URL 多久會失效?我應該如何永久儲存圖片?
A: 圖片 URL 的有效期間為 24 小時。您必須在擷取到 URL 後,立即通過程式下載圖片,並將其儲存到您自己的持久化儲存中,例如本機伺服器或阿里雲Object Storage Service。
Q: 調用API返回DataInspectionFailed錯誤,如何處理?
A: 該錯誤表示輸入文本觸發了Alibaba Content Security Service審核。請檢查並修改prompt或negative_prompt中的文本,移除可能違規的內容後重試。
Q: prompt_extend參數應該開啟還是關閉?
A: 當輸入的prompt比較簡潔或希望模型發揮更多創意時,建議保持開啟(預設)。當prompt已經非常詳細、專業,或對API響應延遲有嚴格要求時,建議顯式設定為false。
Q: 如何增益圖像中文字的產生效果?
A: 如果業務強依賴於在映像中產生清晰、準確的文字,請使用qwen-image-plus模型,它是為此類情境專門訓練的。











