文本生成图像

更新时间:2025-04-29 09:10:03

通义万相-文生图模型支持通过一句话生成图像,分为V2版和V1版。全面升级的文生图V2版模型提升了语义理解能力,通过预置智能改写功能帮助您快速上手图像创作。此外,V2版支持任意分辨率,输出图像最高可达200万像素。推荐优先使用文生图V2版模型。

模型概览及对比

模型概览

文生图V2版
文生图V1版

您可以选择全面升级的通义万相-文生图V2系列模型进行文生图创作,请使用通义万相官网体验效果。

  • 文生图2.1系列模型:wanx2.1-t2i-turbo、wanx2.1-t2i-plus。

  • 文生图2.0系列模型:wanx2.0-t2i-turbo。

image

基础文生图模型,支持设置文生图的风格,支持基于参考图像进行二次创作,生成相似风格的图像。

  • 文生图1.0系列模型:wanx-v1。

image

模型版本

模型名称

模型简介

模型版本

模型名称

模型简介

通义万相文生图2.1

(文生图V2版)

wanx2.1-t2i-plus

全面升级版本。生成图像细节更丰富,速度稍慢。对应通义万相官网2.1专业模型。

wanx2.1-t2i-turbo

全面升级版本。生成速度快、效果全面、综合性价比高。对应通义万相官网2.1极速模型。

通义万相文生图2.0

(文生图V2版)

wanx2.0-t2i-turbo

擅长质感人像,速度中等、成本较低。对应通义万相官网2.0极速模型。

通义万相文生图1.0

(文生图V1版)

wanx-v1

基础文生图模型。对应通义万相官网1.0通用模型。

模型名称

计费单价

限流(主账号与RAM子账号共用)

免费额度

任务下发接口RPS限制

同时处理中任务数量

模型名称

计费单价

限流(主账号与RAM子账号共用)

免费额度

任务下发接口RPS限制

同时处理中任务数量

wanx2.1-t2i-plus

0.20元/张

2

2

免费额度:500

有效期:阿里云百炼开通后180天内

wanx2.1-t2i-turbo

0.14元/张

2

2

wanx2.0-t2i-turbo

0.04元/张

2

2

wanx-v1

0.16元/张

2

1

模型效果对比

下表对比了文生图三个模型的生成效果,仅调整prompt,其他参数均使用API默认值。

模型建议:推荐优先选择文生图V2版模型。

  • 若需要高质量图像,选择文生图2.1(wanx2.1-t2i-turbowanx2.1-t2i-plus),其中wanx2.1-t2i-turbo更具性价比。

  • 若考虑成本,可选择文生图2.0(wanx2.0-t2i-turbo)。

文生图2.1(wanx2.1-t2i-turbo)

文生图2.0(wanx2.0-t2i-turbo)

文生图1.0(wanx- v1)

文生图2.1(wanx2.1-t2i-turbo)

文生图2.0(wanx2.0-t2i-turbo)

文生图1.0(wanx- v1)

prompt = "生成一张新年祝福贺卡,背景有白雪,放鞭炮的小孩,蛇形成文案2025,并写上HAPPY NEW YEAR。"

效果对比:文生图2.1模型的文字生成能力更强,适合创意设计场景。

010f8af7-f20e-49ed-a710-ea61d392a38d-1

b01ad031-296d-490f-9c66-45ff3eb4a51e-1

01822ce1-eb01-449c-9771-7bdb646f0e1e-1

prompt = "一个用针毡制作的圣诞老人手持礼物旁边站着一只白色的猫咪,背景中有许多五颜六色的礼物。整个场景应该是可爱、温暖和舒适的,并且背景中还有一些绿色植物。"

效果对比:文生图2.1模型的语义理解更加准确(如手持礼物),生成的图像质量更高。

bb1c4ecb-d89e-4ca0-a57e-c3f9a4de645b-1

6e4fb493-b792-4fc1-b447-c312dc5975d0-1

aab9ff32-27d6-4afd-82ab-09faa6b9f5a4-1

prompt = "中国女孩,圆脸,看着镜头,优雅的民族服装,商业摄影,室外,电影级光照,半身特写,精致的淡妆,锐利的边缘。 "

效果对比:文生图2.0模型在质感人像生成方面表现出色,其成本仅为文生图2.1模型的三分之一,性价比高。

0483afe7b64f4127b983e58cbb119b24_1

861503c7476648448ac3c5ff9d651e4a_0

362f0a659e5442a2aa05cb77fd5f61af_3

prompt = "高清摄影写真,一只布偶猫慵懒地躺在复古木质窗台上。它拥有一身柔软的银白色长毛,深蓝色宝石般的眼睛,粉嫩的小鼻头和肉垫。猫咪眼神温柔地望向镜头,嘴角似乎带着一抹满足的微笑。午后阳光透过半开的窗户洒在它身上,营造出温馨而宁静的氛围。背景是模糊的绿色植物和老式窗帘,增添了几分生活气息。近景特写,自然光影效果。"

效果对比:文生图2.1模型在动物图像的生成和理解方面更具优势,其他两个模型没有生成布偶猫,1.0模型生成的动物图像质量较差。

e22f46d6e492474795bc251dda2470f3_0

13ed17a5d05f4a38be0720cd24f36004_3

c603b45080bf4b2d8e10964da306e5f3_2

示例代码

文生图2.1(wanx2.1-t2i-turbo)
文生图2.0(wanx2.0-t2i-turbo)
文生图1.0(wanx- v1)
curl -X POST https://dashscope.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": "wanx2.1-t2i-turbo",
    "input": {
        "prompt": "生成一张新年祝福贺卡,背景有白雪,放鞭炮的小孩,蛇形成文案2025,并写上HAPPY NEW YEAR。"
    }
}'    
curl -X POST https://dashscope.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": "wanx2.0-t2i-turbo",
    "input": {
        "prompt": "生成一张新年祝福贺卡,背景有白雪,放鞭炮的小孩,蛇形成文案2025,并写上HAPPY NEW YEAR。"
    }
}'    
curl -X POST https://dashscope.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": "wanx-v1",
    "input": {
        "prompt": "生成一张新年祝福贺卡,背景有白雪,放鞭炮的小孩,蛇形成文案2025,并写上HAPPY NEW YEAR。"
    }
}'    

前期准备

文生图V2V1版模型API均支持通过HTTPDashScope SDK进行调用。

在调用前,您需要开通模型服务并获取API Key,再配置API Key到环境变量

如需通过SDK进行调用,请安装DashScope SDK。目前,该SDK已支持PythonJava。

下面展示文生图V2版和V1版的HTTP请求参数。

文生图V2版
文生图V1版
{
    "model": "", //必填项
    "input": {
        "prompt": "", //提示词,必填项
        "negative_prompt": "" //选填项
    },
    "parameters": {
        "size": "1024*1024", //默认值
        "n": 4, //默认值
        "prompt_extend": true, //默认值
        "watermark": false, //默认值
        "seed": 42 //示例值
    }
}
{
    "model": "", //必填项
    "input": {
        "prompt": "", //提示词,必填项
        "negative_prompt": "", //选填项
        "ref_img": "" //选填项
    },
    "parameters": {
        "style": "<auto>", //默认值
        "size": "1024*1024", //默认值
        "n": 4, //默认值
        "ref_strength": 0.5, //默认值
        "ref_mode": "repaint", //默认值
        "seed": 42 //示例值
    }
}

提示词

文生图模型需要使用一段文字描述生成的图片。提示词(prompt)描述越完整、精确和丰富,生成的图像品质越高,越贴近期望生成的内容。

提示词撰写技巧请参见文生图Prompt指南

关键能力-文生图V2

通义万相-文生图V2版模型的升级要点如下所示。参数说明请参见通义万相-文生图V2

  • prompt智能改写:默认开启智能改写,对于较短的输入prompt生成效果提升明显。推荐使用默认值。

  • 按任意分辨率生成图像:文生图V1版模型仅支持4种输出图像分辨率,V2版模型支持设置任意分辨率,最高可达200万像素。

  • 添加水印:开放水印参数,可为输出图像添加水印。默认不添加水印。

prompt智能改写

通过prompt_extend控制是否开启prompt智能改写。默认开启智能改写,开启后会使用大模型(qwen模型)对输入prompt进行智能改写。对于较短的输入prompt生成效果提升明显,但会增加3-4秒耗时。

下面展示两个开启prompt智能改写的前后对比示例,模型使用wanx2.1-t2i-turbo

// prompt示例1
prompt = "火星上的城市,废土风格。"

不开启智能改写

开启智能改写

改写后的prompt

不开启智能改写

开启智能改写

改写后的prompt

a898b904-47bc-413d-8d26-7c131dea093e-1

be4315ce-86d2-4a44-8112-4dd5a0b58ee8-1

废土风格的火星城市,画面中可以看到荒凉的红色沙地和破败的建筑。城市中散布着废弃的车辆和机械残骸,天空呈现出暗淡的橙黄色。远处有几座破损的高楼,表面覆盖着尘土和裂痕。近景处有一群穿着厚重防护服的探险者,他们手持武器,警惕地观察四周。整体色调偏暗,充满末日氛围。长焦镜头拍摄,细节丰富。

81d6072e-2079-4d4a-8eab-4c44ed5131e1-1

3e0404f4-c375-4300-9a6b-2b0109a83352-1

废土风格的火星城市,荒凉的红色地表上散布着废弃的建筑和残破的机械。城市的天际线被尘暴笼罩,远处可见几座破损的高塔。街道上散落着锈迹斑斑的车辆和碎片。画面整体色调偏暗,充满末日氛围。近景中有一辆破旧的越野车停在路边,驾驶舱内坐着一位穿着厚重防护服的探险者,他/她正凝视前方。长焦镜头,写实摄影风格。

示例代码:prompt智能改写

Python
Java
HTTP
from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
from dashscope import ImageSynthesis
import os

prompt = "火星上的城市,废土风格。"

print('----sync call, please wait a moment----')
rsp = ImageSynthesis.call(api_key=os.getenv("DASHSCOPE_API_KEY"),
                          model="wanx2.1-t2i-turbo",
                          prompt=prompt,
                          n=2,
                          prompt_extend=True,
                          size='1024*1024')
print('response: %s' % 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('sync_call Failed, status_code: %s, code: %s, message: %s' %
          (rsp.status_code, rsp.code, rsp.message))
// 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.task.AsyncTaskListParam;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import java.util.HashMap;

public class Main {
    public static void basicCall() throws ApiException, NoApiKeyException {
        String prompt = "火星上的城市,废土风格。";
        HashMap<String,Object> parameters = new HashMap<>();
        parameters.put("prompt_extend", true);
        ImageSynthesisParam param =
                ImageSynthesisParam.builder()
                        .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                        .model("wanx2.1-t2i-turbo")
                        .prompt(prompt)
                        .n(2)
                        .size("1024*1024")
                        .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());
        }
    }
}

HTTP调用仅支持异步获取模型结果,您需要发起两个请求。

  1. 创建任务

此接口返回任务ID,可根据任务ID查询图像生成的结果。

curl -X POST https://dashscope.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": "wanx2.1-t2i-turbo",
    "input": {
        "prompt": "火星上的城市,废土风格。"
    },
    "parameters": {
        "size": "1024*1024",
        "n": 2,
        "prompt_extend": true
    }
}'    
  1. 根据任务ID查询结果

curl -X GET https://dashscope.aliyuncs.com/api/v1/tasks/{your_task_id} \
     -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
注意:需要将{your_task_id}替换为真实的任务ID。
// prompt示例2
prompt = "外景,香水瓶,产品照片,暖色调"

不开启智能改写

开启智能改写

改写后的prompt

不开启智能改写

开启智能改写

改写后的prompt

debfb510-0b3c-4fd8-9b03-dc6fc112e5f0-1

83dd5c1c-1d1d-4697-bc98-db3e768c03b2-1

暖色调外景产品照片,一瓶精致的香水瓶置于画面中央。香水瓶身透明,内部液体呈淡金色,瓶盖为金色金属质感。背景是温暖的夕阳下,绿树成荫的小径和远处的建筑。光线柔和,营造出温馨舒适的氛围。高清写实摄影,近景特写构图。

77aaed4b-358b-4a5a-abf6-8e1c25901956-1

7ba0fa17-1f23-400f-a610-e3c0d75d0853-1

暖色调外景产品照片,一瓶精致的香水瓶置于画面中央。香水瓶身透明,内装淡粉色液体,瓶颈装饰有金色细节。背景是柔和的自然景观,可见蓝天、白云和远处的树木。光线温暖,营造出温馨浪漫的氛围。高清写实摄影,近景特写构图。

按任意分辨率生成图像

通过设置 size 参数来控制输出图像的分辨率。

升级版的文生图V2版模型支持自定义设置图像分辨率,图像宽高边长的像素范围为:[768, 1440],默认值是1024*1024。

下面展示常见的输出图像分辨率尺寸,模型使用wanx2.1-t2i-plus

size=768*1024(3:4)

size=1024*768(4:3)

size=1024*1024(1:1)

size=768*1024(3:4)

size=1024*768(4:3)

size=1024*1024(1:1)

4adfea57-8b53-4bd3-9a4e-eaeff2b2cd68-1

45f5d983-de30-44eb-a474-69d60563a35b-1

e4437f81-7188-4cd8-bbda-070341e8a87b-1

示例代码:设置图像分辨率

Python
Java
HTTP
from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
from dashscope import ImageSynthesis
import os

prompt = "国漫风格穿越少女,在古代宫廷中烛光照耀下学习礼仪一举一动皆显古风优雅"

print('----sync call, please wait a moment----')
rsp = ImageSynthesis.call(api_key=os.getenv("DASHSCOPE_API_KEY"),
                          model="wanx2.1-t2i-plus",
                          prompt=prompt,
                          n=2,
                          prompt_extend=True,
                          size='768*1024')
print('response: %s' % 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('sync_call Failed, status_code: %s, code: %s, message: %s' %
          (rsp.status_code, rsp.code, rsp.message))
// 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.task.AsyncTaskListParam;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import java.util.HashMap;

public class Main {
    public static void basicCall() throws ApiException, NoApiKeyException {
        String prompt = "国漫风格穿越少女,在古代宫廷中烛光照耀下学习礼仪一举一动皆显古风优雅";
        HashMap<String,Object> parameters = new HashMap<>();
        parameters.put("prompt_extend", true);
        ImageSynthesisParam param =
                ImageSynthesisParam.builder()
                        .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                        .model("wanx2.1-t2i-plus")
                        .prompt(prompt)
                        .n(2)
                        .size("768*1024")
                        .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());
        }
    }
}

HTTP调用仅支持异步获取模型结果,您需要发起两个请求。

  1. 创建任务

此接口返回任务ID,可根据任务ID查询图像生成的结果。

curl -X POST https://dashscope.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": "wanx2.1-t2i-plus",
    "input": {
        "prompt": "国漫风格穿越少女,在古代宫廷中烛光照耀下学习礼仪一举一动皆显古风优雅"
    },
    "parameters": {
        "size": "768*1024",
        "n": 2,
        "prompt_extend": true
    }
}'    
  1. 根据任务ID查询结果

curl -X GET https://dashscope.aliyuncs.com/api/v1/tasks/{your_task_id} \
     -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
注意:需要将{your_task_id}替换为真实的任务ID。

添加水印

通过watermark为输出图像添加水印标识,水印位于图片右下角,文案为“AI生成”。默认值为false,表示不添加水印。

下面展示添加水印的效果,模型使用wanx2.1-t2i-turbo

watermark=false(不添加水印)

watermark=true(添加水印)

watermark=false(不添加水印)

watermark=true(添加水印)

0102e110-2432-48a3-a5c4-e640272e1f1c-1

ebd61160-c3d7-4687-8e42-34b2874d7b44-1

示例代码:添加水印

Python
Java
HTTP
from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
from dashscope import ImageSynthesis
import os

prompt = "浅水彩,咖啡馆外,明亮的白色背景,梦幻,吉卜力工作室。"

print('----sync call, please wait a moment----')
rsp = ImageSynthesis.call(api_key=os.getenv("DASHSCOPE_API_KEY"),
                          model="wanx2.1-t2i-turbo",
                          prompt=prompt,
                          n=2,
                          prompt_extend=True,
                          watermark=True,
                          size='1024*1024')
print('response: %s' % 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('sync_call Failed, status_code: %s, code: %s, message: %s' %
          (rsp.status_code, rsp.code, rsp.message))
// 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.task.AsyncTaskListParam;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import java.util.HashMap;

public class Main {
    public static void basicCall() throws ApiException, NoApiKeyException {
        String prompt = "浅水彩,咖啡馆外,明亮的白色背景,梦幻,吉卜力工作室。";
        HashMap<String,Object> parameters = new HashMap<>();
        parameters.put("prompt_extend", true);
        parameters.put("watermark", true);
        ImageSynthesisParam param =
                ImageSynthesisParam.builder()
                        .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                        .model("wanx2.1-t2i-turbo")
                        .prompt(prompt)
                        .n(1)
                        .size("1024*1024")
                        .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());
        }
    }
}

HTTP调用仅支持异步获取模型结果,您需要发起两个请求。

  1. 创建任务

此接口返回任务ID,可根据任务ID查询图像生成的结果。

curl -X POST https://dashscope.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": "wanx2.1-t2i-turbo",
    "input": {
        "prompt": "浅水彩,咖啡馆外,明亮的白色背景,梦幻,吉卜力工作室。"
    },
    "parameters": {
        "size": "1024*1024",
        "n": 1,
        "prompt_extend": true,
        "watermark": true
    }
}'    
  1. 根据任务ID查询结果

curl -X GET https://dashscope.aliyuncs.com/api/v1/tasks/{your_task_id} \
     -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
注意:需要将{your_task_id}替换为真实的任务ID。

关键能力-文生图V1

通义万相-文生图V1版模型支持:根据输入文本生成图像、根据输入文本和参考图像生成相似图。参数说明请参见通义万相-文生图V1

文生图V1版模关键能力包括:

  • 使用反向提示词:支持使用反向提示词,来指定图像中不包含的元素。

  • 基于参考图像生成相似图ref_mode指定生成相似图的方式,repaint代表参考内容,默认值;refonly代表参考风格。

使用反向提示词

通过正向提示词描述期望的图像元素和风格,使用反向提示词来描述不希望在图像中看到的内容。例如,设置negative_prompt以限制输出图像中不要使用红色元素。

//正向提示词
prompt = "近景镜头,18岁的中国女孩,古代服饰,圆脸,正面看着镜头,民族优雅的服装,商业摄影,室外,电影级光照,半身特写,精致的淡妆,锐利的边缘。"

//反向提示词
negative_prompt = "红色元素"

image

示例代码:使用反向提示词

Python
Java
HTTP
from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
from dashscope import ImageSynthesis
import os

prompt = "近景镜头,18岁的中国女孩,古代服饰,圆脸,正面看着镜头,民族优雅的服装,商业摄影,室外,电影级光照,半身特写,精致的淡妆,锐利的边缘。"
negative_prompt = "红色元素"

# 同步调用
print('----sync call, please wait a moment----')

rsp = ImageSynthesis.call(api_key=os.getenv("DASHSCOPE_API_KEY"),
                          model=ImageSynthesis.Models.wanx_v1,
                          prompt=prompt,
                          negative_prompt=negative_prompt,
                          n=1,
                          style='<auto>',
                          size='1024*1024')
if rsp.status_code == HTTPStatus.OK:
    print(rsp.output)

    # 保存图片
    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('sync_call Failed, status_code: %s, code: %s, message: %s' %
          (rsp.status_code, rsp.code, rsp.message))
import com.alibaba.dashscope.aigc.imagesynthesis.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;

public class Main {

    public void syncCall() {
        String prompt = "近景镜头,18岁的中国女孩,古代服饰,圆脸,正面看着镜头,民族优雅的服装,商业摄影,室外,电影级光照,半身特写,精致的淡妆,锐利的边缘。";
        String negativePrompt = "红色元素";
        ImageSynthesisParam param =
                ImageSynthesisParam.builder()
                        .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                        .model(ImageSynthesis.Models.WANX_V1)
                        .prompt(prompt)
                        .negativePrompt(negativePrompt)
                        .style("<auto>")
                        .n(1)
                        .size("1024*1024")
                        .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){
        Main main = new Main();
        main.syncCall();
    }

}

HTTP调用仅支持异步获取模型结果,您需要发起两个请求。

  1. 创建任务

此接口返回任务ID,可根据任务ID查询图像生成的结果。

curl -X POST https://dashscope.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": "wanx-v1",
    "input": {
        "prompt": "近景镜头,18岁的中国女孩,古代服饰,圆脸,正面看着镜头,民族优雅的服装,商业摄影,室外,电影级光照,半身特写,精致的淡妆,锐利的边缘。",
        "negative_prompt": "红色元素"
    },
    "parameters": {
        "style": "<auto>",
        "size": "1024*1024",
        "n": 1
    }
}'
    
  1. 根据任务ID查询结果

curl -X GET https://dashscope.aliyuncs.com/api/v1/tasks/{your_task_id} \
     -H "Authorization: Bearer $DASHSCOPE_API_KEY" \

基于参考图生成相似图

基于参考图生成图像涉及两个参数:ref_strength参数和ref_mode参数。

设置相似度

您可以使用ref_strength参数来控制输出图像与参考图的相似度。它的取值范围是[0.0, 1.0],默认值为0.5。取值越大表示跟参考图像越相似。

image

示例代码:设置相似度

Python
Java
curl
from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
from dashscope import ImageSynthesis
import os


prompt = "近景镜头,18岁的中国女孩,古代服饰,圆脸,正面看着镜头,民族优雅的服装,商业摄影,室外,电影级光照,半身特写,精致的淡妆,锐利的边缘。"

# 上传参考图方式:url链接和本地路径二选一
# 使用公网url链接
ref_img = "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241031/rguyzt/girl.png"
# 使用本地文件路径
sketch_image_url = './girl.png'

# 同步调用
print('----sync call, please wait a moment----')

rsp = ImageSynthesis.call(api_key=os.getenv("DASHSCOPE_API_KEY"),
                          model=ImageSynthesis.Models.wanx_v1,
                          prompt=prompt,
                          n=1,
                          style='<auto>',
                          # sketch_image_url=sketch_image_url,
                          ref_img=ref_img,
                          ref_mode='repaint',
                          ref_strength=0.0,
                          size='1024*1024')
if rsp.status_code == HTTPStatus.OK:
    print(rsp.output)

    # 保存图片
    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('sync_call Failed, status_code: %s, code: %s, message: %s' %
          (rsp.status_code, rsp.code, rsp.message))
import com.alibaba.dashscope.aigc.imagesynthesis.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import java.util.HashMap;

public class Main {

    public void syncCall() {
        String prompt = "近景镜头,18岁的中国女孩,古代服饰,圆脸,正面看着镜头,民族优雅的服装,商业摄影,室外,电影级光照,半身特写,精致的淡妆,锐利的边缘。";
         //使用公网url链接
        String refImage = "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241031/rguyzt/girl.png";

        //图像处理参数
        HashMap<String,Object> parameters = new HashMap<>();
        parameters.put("ref_strength", 0.0);
        parameters.put("ref_mode", "repaint");
        
        ImageSynthesisParam param =
                ImageSynthesisParam.builder()
                        .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                        .model(ImageSynthesis.Models.WANX_V1)
                        .prompt(prompt)
                        .style("<auto>")
                        .n(1)
                        .size("1024*1024")
                        .refImage(refImage)
                        .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){
        Main main = new Main();
        main.syncCall();
    }

}

HTTP调用仅支持异步获取模型结果,您需要发起两个请求。

1、创建文生图任务

此接口返回任务ID,可根据任务ID查询图像生成的结果。

curl -X POST https://dashscope.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": "wanx-v1",
    "input": {
        "prompt": "近景镜头,18岁的中国女孩,古代服饰,圆脸,正面看着镜头,民族优雅的服装,商业摄影,室外,电影级光照,半身特写,精致的淡妆,锐利的边缘。",
        "ref_image": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241031/rguyzt/girl.png"
    },
    "parameters": {
        "style": "<auto>",
        "size": "1024*1024",
        "n": 1,
        "ref_strength": 0.0,
        "ref_mode": "repaint"
    }
}'
    

2、根据任务ID查询结果

curl -X GET https://dashscope.aliyuncs.com/api/v1/tasks/{your_task_id} \
     -H "Authorization: Bearer $DASHSCOPE_API_KEY" \

设置相似图的生成方式

您可以通过ref_mode参数来控制如何基于参考图像生成相似图。

  • ref_mode="repaint":根据参考图像内容生成图像,默认值。

  • ref_mode="refonly":根据参考图像风格生成图像。

image

示例代码:设置相似图的生成方式

Python
Java
HTTP
from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
from dashscope import ImageSynthesis
import os

prompt = "近景镜头,18岁的中国女孩,古代服饰,圆脸,正面看着镜头,民族优雅的服装,商业摄影,室外,电影级光照,半身特写,精致的淡妆,锐利的边缘。"

# 上传参考图方式:url链接和本地路径二选一
# 使用公网url链接
ref_img = "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241031/rguyzt/girl.png"
# 使用本地文件路径
sketch_image_url = './girl.png'

# 同步调用
print('----sync call, please wait a moment----')

rsp = ImageSynthesis.call(api_key=os.getenv("DASHSCOPE_API_KEY"),
                          model=ImageSynthesis.Models.wanx_v1,
                          prompt=prompt,
                          n=4,
                          style='<auto>',
                          # sketch_image_url=sketch_image_url,
                          ref_img=ref_img,
                          ref_mode='repaint',
                          ref_strength=1.0,
                          size='1024*1024')
if rsp.status_code == HTTPStatus.OK:
    print(rsp.output)

    # 保存图片
    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('sync_call Failed, status_code: %s, code: %s, message: %s' %
          (rsp.status_code, rsp.code, rsp.message))
import com.alibaba.dashscope.aigc.imagesynthesis.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import java.util.HashMap;

public class Main {

    public void syncCall() {
        String prompt = "近景镜头,18岁的中国女孩,古代服饰,圆脸,正面看着镜头,民族优雅的服装,商业摄影,室外,电影级光照,半身特写,精致的淡妆,锐利的边缘。";
         
         //使用公网url链接
        String refImage = "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241031/rguyzt/girl.png";

        //图像处理参数
        HashMap<String,Object> parameters = new HashMap<>();
        parameters.put("ref_strength", 1.0);
        parameters.put("ref_mode", "repaint");
        
        ImageSynthesisParam param =
                ImageSynthesisParam.builder()
                        .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                        .model(ImageSynthesis.Models.WANX_V1)
                        .prompt(prompt)
                        .style("<auto>")
                        .n(1)
                        .size("1024*1024")
                        .refImage(refImage)
                        .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){
        Main main = new Main();
        main.syncCall();
    }

}

HTTP调用仅支持异步获取模型结果,您需要发起两个请求。

  1. 创建任务

此接口返回任务ID,可根据任务ID查询图像生成的结果。

curl -X POST https://dashscope.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": "wanx-v1",
    "input": {
        "prompt": "近景镜头,18岁的中国女孩,古代服饰,圆脸,正面看着镜头,民族优雅的服装,商业摄影,室外,电影级光照,半身特写,精致的淡妆,锐利的边缘。",
        "ref_image": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241031/rguyzt/girl.png"
    },
    "parameters": {
        "style": "<auto>",
        "size": "1024*1024",
        "n": 1,
        "ref_strength": 1.0,
        "ref_mode": "repaint"
    }
}'
    
    

2、根据任务ID查询结果

curl -X GET https://dashscope.aliyuncs.com/api/v1/tasks/{your_task_id} \
     -H "Authorization: Bearer $DASHSCOPE_API_KEY" \

相关文档

  • 本页导读 (1)
  • 模型概览及对比
  • 模型概览
  • 模型效果对比
  • 前期准备
  • 提示词
  • 关键能力-文生图V2版
  • prompt智能改写
  • 按任意分辨率生成图像
  • 添加水印
  • 关键能力-文生图V1版
  • 使用反向提示词
  • 基于参考图生成相似图
  • 相关文档