通过文生图API,您可以基于文本描述创造出全新的图像。阿里云百炼提供两大系列模型:
通义千问(Qwen-Image): 擅长渲染复杂的中英文文本。
通义万相(Wan系列): 用于生成写实图像和摄影级视觉效果。
模型效果
通义千问(Qwen-image)
复杂文字
| 超长段落
| 复杂布局
|
海报制作
| 插画设计
| 写实摄影
|
通义万相
人像写真
| 写实摄影
| 绘画流派
|
文字生成
| 海报设计
| 组图生成
|
支持的模型
模型选型
复杂文字渲染(如海报、对联):首选
qwen-image-max、wan2.6-t2i。写实场景和摄影风格(通用场景):可选通义万相模型,如
wan2.6-t2i、wan2.5-t2i-preview。需要自定义输出图像分辨率:推荐通义万相模型,如
wan2.2-t2i-flash,支持 [512, 1440] 像素范围内的任意宽高组合。通义千问Qwen-Image仅支持5种固定尺寸:1664*928(16:9)、928*1664(9:16)、1328*1328(1:1)、1472*1104(4:3)、1104*1472(3:4)。
快速开始
前提条件
在调用前,请获取API Key,再配置API Key到环境变量。如果通过DashScope SDK进行调用,还需要安装SDK。
示例代码
调用方式说明:通义千问Qwen-Image支持同步调用,其中qwen-image-plus、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", # 当前仅qwen-image-plus、qwen-image模型支持异步接口
prompt=prompt,
negative_prompt=" ",
n=1,
size='1664*928',
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);
parameters.put("negative_prompt", " ");
ImageSynthesisParam param =
ImageSynthesisParam.builder()
.apiKey(apiKey)
// 当前仅qwen-image-plus、qwen-image模型支持异步接口
.model("qwen-image-plus")
.prompt(prompt)
.n(1)
.size("1664*928")
.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": {
"negative_prompt":" ",
"size": "1664*928",
"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替换为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 种固定的分辨率:
1664*928(默认值):16:9。
1472*1104:4:3 。
1328*1328:1:1。
1104*1472:3:4。
928*1664:9:16。
通义万相 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 后立即下载图片,并转存至您自己的持久化存储服务中(如阿里云对象存储 OSS)。
内容安全审核:所有
prompt和negative_prompt都会经过内容安全审核。若输入内容不合规,请求将被拦截并返回DataInspectionFailed错误。生成内容的版权与合规风险:请确保您的提示词内容符合相关法律法规。生成包含品牌商标、名人肖像、受版权保护的 IP 形象等内容可能涉及侵权风险,请您自行评估并承担相应责任。
API文档
计费与限流
模型免费额度和计费单价请参见图像生成。
计费规则
计费项:按成功生成的 图像张数 计费,采用按量后付费模式。
计费公式:费用 = 计费单价 × 图像张数。
抵扣顺序:优先消耗免费额度。额度用尽后,默认转为按量付费。
您可开启“免费额度用完即停”功能,以避免免费额度耗尽后产生额外费用。详情请参见免费额度。
失败不计费:模型调用失败或处理错误不产生任何费用,也不消耗免费额度。
免费额度
关于免费额度的领取、查询、使用方法等详情,请参见免费额度。
调用量查询
模型调用完约一小时后,请在模型观测(新加坡)页面,查看调用量、调用次数、成功率等指标。
如果使用“华北2(北京)”地域的模型,请前往“华北2(北京)”地域的模型观测页面。
限流
模型限流规则及常见问题,请参见限流。
错误码
如果模型调用失败并返回报错信息,请参见错误信息进行解决。
常见问题
Q: 图片 URL 多久会失效?我应该如何永久保存图片?
A: 图片 URL 的有效期为 24 小时。您必须在获取到 URL 后,立即通过程序下载图片,并将其保存到您自己的持久化存储中,例如本地服务器或阿里云对象存储 OSS。
Q: 调用API返回DataInspectionFailed错误,如何处理?
A: 该错误表示输入文本触发了内容安全审核。请检查并修改prompt或negative_prompt中的文本,移除可能违规的内容后重试。
Q: prompt_extend参数应该开启还是关闭?
A: 当输入的prompt比较简洁或希望模型发挥更多创意时,建议保持开启(默认)。当prompt已经非常详细、专业,或对API响应延迟有严格要求时,建议显式设置为false。
Q: 如何提升图像中文字的生成效果?
A: 如果业务强依赖于在图像中生成清晰、准确的文字,请使用qwen-image-plus模型,它是为此类场景专门训练的。











