Skip to main content

千问-文生图API参考

千问-文生图模型(Qwen-Image)是一款通用图像生成模型,支持多种艺术风格,尤其擅长 复杂文本渲染 。模型支持多行布局、段落级文本生成以及细粒度细节刻画,可实现复杂的图文混合布局设计。

快速入口:使用指南 | 在线体验(新加坡 | 北京) | 技术博客(更多效果展示)

效果展示

输入提示词

输出图像

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.

image

模型概览

模型名称模型简介输出图像规格
qwen-image-2.0-pro 推荐
当前与qwen-image-2.0-pro-2026-04-22能力相同
千问图像生成与编辑模型Pro系列。文字渲染、真实质感、语义遵循能力更强。
图像编辑请参考千问-图像编辑。
图像分辨率:支持自由设置宽高,输出图像总像素需在512512至20482048之间。默认分辨率为2048*2048。图像格式:png图像张数:1-6张
qwen-image-2.0-pro-2026-06-22 推荐
qwen-image-2.0-pro-2026-04-22
qwen-image-2.0-pro-2026-03-03
qwen-image-2.0 推荐
当前与qwen-image-2.0-2026-03-03能力相同
千问图像生成与编辑模型加速版,兼顾效果与响应速度。
图像编辑请参考千问-图像编辑。
qwen-image-2.0-2026-03-03 推荐
各地域支持的模型请参见百炼控制台。

前提条件

在调用前,先获取与配置 API Key,再配置API Key到环境变量。如需通过SDK进行调用,请安装DashScope SDK。
华北2(北京)和新加坡地域拥有独立的 API Key与请求地址,不可混用,跨地域调用将导致鉴权失败或服务报错。
阿里云百炼为华北2(北京)、新加坡地域推出了业务空间专属域名,能够为推理请求提供卓越的性能和更高的稳定性,建议迁移至新域名:
  • 华北2(北京)地域:从 https://dashscope.aliyuncs.com 迁移至 https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com
  • 新加坡地域:从 https://dashscope-intl.aliyuncs.com 迁移至 https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com
其中 {WorkspaceId} 为您的业务空间 ID,可在阿里云百炼控制台的业务空间详情页面查看。现有域名仍可正常使用。

同步接口

HTTP调用

千问图像模型支持同步接口,一次请求即可获得结果,调用流程简单,推荐用于多数场景。 新加坡地域:POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation 北京地域:POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation 调用时请将{WorkspaceId}替换为真实的Workspace ID。

请求参数

请求头(Headers)
Content-Typestring(必选)请求内容类型。此参数必须设置为application/json。Authorizationstring(必选)请求身份认证。接口使用阿里云百炼API Key进行身份认证。示例值:Bearer sk-xxxx。
请求体(Request Body)
model string (必选)模型名称。示例值:qwen-image-2.0-pro。input object (必选)输入的基本信息。

属性

messages array (必选)请求内容数组。当前仅支持单轮对话,数组内有且只有一个元素。

属性

rolestring (必选)消息的角色。此参数必须设置为user。contentarray (必选)消息内容数组。

属性

textstring(必选)正向提示词用于描述您期望生成的图像内容、风格和构图。支持中英文,qwen-image-2.0系列模型长度上限为 1300 Token,其他模型为 800 Token,超出部分将自动截断。注意:仅支持传入一个text,不传或传入多个将报错。
parameters object (可选)图像处理参数。

属性

negative_prompt string (可选)反向提示词,用于描述不希望在图像中出现的内容,对画面进行限制。支持中英文,长度不超过500个字符,超出部分将自动截断。示例值:低分辨率,低画质,肢体畸形,手指畸形,画面过饱和,蜡像感,人脸无细节,过度光滑,画面具有AI感。构图混乱。文字模糊,扭曲。size string (可选)输出图像的分辨率,格式为宽*高。qwen-image-2.0系列模型:输出图像总像素需在512*512至2048*2048之间,默认分辨率为2048*2048。推荐分辨率:
  • 2688*1536 :16:9
  • 1536*2688 :9:16
  • 2048*2048(默认值):1:1
  • 2368*1728 :4:3
  • 1728*2368 :3:4
n integer (可选)输出图像的数量,默认值为1。对于qwen-image-2.0系列模型,可选择输出1-6张图片。prompt_extend bool (可选)是否开启 Prompt(提示词)智能改写功能。开启后模型将对正向提示词进行优化与润色。此功能不会修改反向提示词。
  • true:默认值,开启智能改写。如果希望图像内容更多样化,由模型补充细节,建议开启此选项。
  • false:关闭智能改写。如果图像细节更可控,建议关闭此选项,并参考文生图Prompt指南进行优化,
原始提示词(orig_prompt):A sitting ginger cat with a joyful expression, looking lively, adorable, and incredibly lifelike.实际提示词(actual_prompt):Photorealistic wildlife portrait: A sitting ginger cat with lush, sun-kissed fur and a vibrant, joyful expression, eyes sparkling with curiosity and warmth. The cat's head is slightly tilted, ears perked forward, mouth gently open in a contented, lively pose—exuding adorable charm and incredible lifelike detail. Soft natural lighting enhances the texture of each individual whisker and fur strand, with subtle highlights on the muzzle and inner ears. Background: a softly blurred autumn garden at golden hour—warm tones of amber leaves, dappled sunlight filtering through trees, and faint hints of mossy stone steps. Composition emphasizes the cat as the central focal point, captured in sharp focus with shallow depth of field. Style: ultra-realistic photography, National Geographic-level detail, 8K resolution, Canon EOS R5, f/1.2 aperture, cinematic lighting, true-to-life color grading, minimal post-processing. No text or overlays.
watermark bool (可选)是否在图像右下角添加 "Qwen-Image" 水印。默认值为 false。水印样式:1seed integer (可选)随机数种子,取值范围[0,2147483647]。使用相同的seed参数值可使生成内容保持相对稳定。若不提供,算法将自动使用随机数种子。注意:模型生成过程具有概率性,即使使用相同的seed,也不能保证每次生成结果完全一致。
  • 文生图
curl --location 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--data '{
    "model": "qwen-image-2.0-pro",
    "input": {
      "messages": [
        {
          "role": "user",
          "content": [
            {
              "text": "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": "低分辨率,低画质,肢体畸形,手指畸形,画面过饱和,蜡像感,人脸无细节,过度光滑,画面具有AI感。构图混乱。文字模糊,扭曲。",
      "prompt_extend": true,
      "watermark": false,
      "size": "2048*2048"
    }
}'

响应参数

output object任务输出信息。

属性

choices array模型生成的输出内容。此数组仅包含一个元素。

属性

finish_reason string任务停止原因,自然停止时为stop。message object模型返回的消息。

属性

rolestring消息的角色,固定为assistant。contentarray

属性

image string生成图像的 URL,图像格式为PNG。链接有效期为24小时,请及时下载并保存图像。
task_metric object任务结果统计。使用qwen-image-2.0系列时无此返回值。

属性

TOTAL integer总的任务数。SUCCEEDED integer任务状态为成功的任务数。FAILED integer任务状态为失败的任务数。
usage object输出信息统计。只对成功的结果计数。

属性

image_count integer模型生成图像的数量,当前固定为1。width integer模型生成图像的宽度(像素)。height integer模型生成图像的高度(像素)。
request_idstring请求唯一标识。可用于请求明细溯源和问题排查。codestring请求失败的错误码。请求成功时不会返回此参数,详情请参见错误码。messagestring请求失败的详细信息。请求成功时不会返回此参数,详情请参见错误码。
  • 任务执行成功
  • 任务执行异常
图像URL仅保留24小时,超时后会被自动清除,请及时保存生成的图像。
{
    "output": {
        "choices": [
            {
                "finish_reason": "stop",
                "message": {
                    "content": [
                        {
                            "image": "https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/xxx.png?Expires=xxx"
                        }
                    ],
                    "role": "assistant"
                }
            }
        ]
    },
    "usage": {
        "height": 2048,
        "image_count": 1,
        "width": 2048
    },
    "request_id": "d0250a3d-b07f-49e1-bdc8-6793f4929xxx"
}

DashScope SDK调用

DashScope SDK目前已支持Python和Java。 SDK与HTTP接口的参数名基本一致,参数结构根据语言特性进行封装。同步调用参数说明可参考HTTP调用。
  • Python
  • Java
请先确认已安装最新版DashScope Python SDK,否则可能运行报错:安装SDK。
请求示例
import json
import os
import dashscope

# 以下为新加坡地域的配置,调用时请将 {WorkspaceId} 替换为真实的业务空间ID,各地域的URL不同。
# 新加坡和北京地域的API Key不同。获取API Key:https://www.alibabacloud.com/help/model-studio/get-api-key
# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx"
client = dashscope.Client(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    workspace="{WorkspaceId}",
    region="ap-southeast-1",
)

messages = [
    {
        "role": "user",
        "content": [
            {"text": "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."}
        ]
    }
]

# 1.x 的 prompt_extend/watermark/negative_prompt/size 等参数,2.0 中统一通过 parameters 传入
response = client.multimodal.chat.create(
    model="qwen-image-2.0-pro",
    messages=messages,
    parameters={
        "watermark": False,
        "prompt_extend": True,
        "negative_prompt": "低分辨率,低画质,肢体畸形,手指畸形,画面过饱和,蜡像感,人脸无细节,过度光滑,画面具有AI感。构图混乱。文字模糊,扭曲。",
        "size": "2048*2048",
    },
)

# 2.0 SDK:生成结果从 message.content_parts 读取,图像 part 形如 {"image": URL, "type": "image"}
message = response.choices[0].message
for part in (message.content_parts or []):
    if "image" in part:
        print(json.dumps(part, ensure_ascii=False))
响应示例
图像链接的有效期为24小时,请及时下载图像。
{
    "status_code": 200,
    "request_id": "d2d1a8c0-325f-9b9d-8b90-xxxxxx",
    "code": "",
    "message": "",
    "output": {
        "text": null,
        "finish_reason": null,
        "choices": [
            {
                "finish_reason": "stop",
                "message": {
                    "role": "assistant",
                    "content": [
                        {
                            "image": "https://dashscope-result-wlcb.oss-cn-wulanchabu.aliyuncs.com/xxx.png?Expires=xxx"
                        }
                    ]
                }
            }
        ]
    },
    "usage": {
        "input_tokens": 0,
        "output_tokens": 0,
        "width": 2048,
        "image_count": 1,
        "height": 2048
    }
}

结合 OpenAI Agents SDK 使用

千问-文生图(Qwen-Image)等图像生成模型通过 DashScope 原生接口调用,不支持 OpenAI 兼容(compatible-mode)模式,因此无法直接作为 OpenAI Agents SDK 中 Agent 的推理模型。 在 OpenAI Agents SDK 等智能体框架中,可将图像生成能力封装为工具(function tool),由支持 OpenAI 兼容模式的文本对话模型(如 qwen-plus)作为 Agent 的推理核心进行调度。示例如下: 运行前请先安装依赖:pip install openai-agents dashscope。
import os
import asyncio
import dashscope
from openai import AsyncOpenAI
from agents import Agent, Runner, function_tool, OpenAIChatCompletionsModel

# 从环境变量读取百炼 API Key(若未配置,可直接替换为 api_key="sk-xxx")
API_KEY = os.getenv("DASHSCOPE_API_KEY")

# 以下为新加坡地域的URL,调用时请将 {WorkspaceId} 替换为真实的业务空间ID,各地域的URL不同。
# 图像生成走 DashScope 原生接口;文本对话模型(Agent 推理核心)通过 OpenAI 兼容模式接入
client = dashscope.Client(
    api_key=API_KEY,
    workspace="{WorkspaceId}",
    region="ap-southeast-1",
)
openai_client = AsyncOpenAI(api_key=API_KEY, base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1")

# 将图像生成封装为工具(function tool),供 Agent 调用
@function_tool
def generate_image(prompt: str) -> str:
    """根据文本描述生成一张图片并返回图片 URL。prompt:图片内容的文字描述。"""
    rsp = client.multimodal.chat.create(
        model="qwen-image-2.0-pro",
        messages=[{"role": "user", "content": [{"text": prompt}]}],
    )
    # 2.0 SDK:生成结果从 message.content_parts 读取,图像 part 形如 {"image": URL, "type": "image"}
    message = rsp.choices[0].message
    for part in (message.content_parts or []):
        if "image" in part:
            return part["image"]
    return "生成失败:未返回图像"

agent = Agent(
    name="图像生成助手",
    instructions="你是图像生成助手。当用户想要图片时,调用 generate_image 工具,并把返回的图片 URL 告诉用户。",
    model=OpenAIChatCompletionsModel(model="qwen-plus", openai_client=openai_client),
    tools=[generate_image],
)

async def main():
    result = await Runner.run(agent, "帮我画一只在草地上奔跑的柯基犬")
    print(result.final_output)

if __name__ == "__main__":
    asyncio.run(main())

计费与限流

错误码

如果模型调用失败并返回报错信息,请参见错误码进行解决。

常见问题

Q:prompt_extend参数应该开启还是关闭?

A:如果希望图像内容更多样化,由模型补充细节,建议开启此选项(默认)。如果图像细节更可控,建议关闭此选项,并参考文生图Prompt指南进行优化,

Q:qwen-image-2.0系列模型有什么区别?

A:
  • 图像生成与编辑融合模型:同时支持文生图和图像编辑。
    • qwen-image-2.0-pro、qwen-image-2.0-pro-2026-04-22:当前两者能力相同,Pro系列具备更专业的文字渲染能力、更细腻的真实质感,细腻刻画写实场景,以及更强的语义遵循能力。仅支持同步接口。
    • qwen-image-2.0、qwen-image-2.0-2026-03-03:当前两者能力相同,加速版有效实现了模型效果和性能的最佳平衡。仅支持同步接口。

Q:如何获取图像存储的访问域名白名单?

A: 模型生成的图像存储于阿里云OSS,API将返回一个临时的公网URL。若需要对该下载地址进行防火墙白名单配置,请注意:由于底层存储会根据业务情况进行动态变更,为避免过期信息影响访问,文档不提供固定的OSS域名白名单。如有安全管控需求,请联系客户经理获取最新OSS域名列表。