阿里通义千问之文本生成图像
目录:
- 1、文本生成图像简介
- 2、代码示例
- 3、文生图Prompt指南
- 一、提示词参数
- 二、提示词公式
- 基础公式:
- 进阶公式:
1、文本生成图像简介
通过文生图API,您可以基于文本描述创造出全新的原创图像。阿里云百炼提供两大系列模型:
- 通义千问(Qwen-Image): 擅长渲染复杂的中英文文本。
- 通义万相(Wan系列): 用于生成写实图像和摄影级视觉效果。
2、代码示例
// 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-intl.aliyuncs.com/api/v1Constants.baseHttpApiUrl = "https://dashscope.aliyuncs.com/api/v1";}// 若没有配置环境变量,请用百炼API Key将下行替换为:apiKey="sk-xxx"// 新加坡和北京地域的API Key不同。获取API Key:https://help.aliyun.com/zh/model-studio/get-api-keystatic String apiKey = System.getenv("DASHSCOPE_API_KEY");public static void basicCall() throws ApiException, NoApiKeyException {String prompt = "一副典雅庄重的对联悬挂于厅堂之中,房间是个安静古典的中式布置,桌子上放着一些青花瓷,对联上左书“义本生知人机同道善思新”,右书“通云赋智乾坤启数高志远”, 横批“智启通义”,字体飘逸,中间挂在一着一副中国风的画作,内容是岳阳楼。";Map<String, Object> parameters = new HashMap<>();parameters.put("prompt_extend", true);parameters.put("watermark", true);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("---同步调用,请等待任务执行----");result = imageSynthesis.call(param);} catch (ApiException | NoApiKeyException e){throw new RuntimeException(e.getMessage());}System.out.println(JsonUtils.toJson(result));}//获取任务列表public static void listTask() throws ApiException, NoApiKeyException {ImageSynthesis is = new ImageSynthesis();AsyncTaskListParam param = AsyncTaskListParam.builder().build();ImageSynthesisListResult result = is.list(param);System.out.println(result);}//根据taskId获取任务结果public void fetchTask() throws ApiException, NoApiKeyException {String taskId = "your task id";ImageSynthesis is = new ImageSynthesis();// 如果设置了 DASHSCOPE_API_KEY 环境变量,apiKey 可以为 nullImageSynthesisResult result = is.fetch(taskId, null);System.out.println(result.getOutput());System.out.println(result.getUsage());}public static void main(String[] args){try{basicCall();//listTask();}catch(ApiException|NoApiKeyException e){System.out.println(e.getMessage());}}
}
响应示例:
url 有效期24小时,请及时下载图像。
{"request_id": "f2153409-3950-9b73-9980-xxxxxx","output": {"task_id": "2fc2e1de-0245-442d-b664-xxxxxx","task_status": "SUCCEEDED","results": [{"orig_prompt": "一副典雅庄重的对联悬挂于厅堂之中,房间是个安静古典的中式布置,桌子上放着一些青花瓷,对联上左书“义本生知人机同道善思新”,右书“通云赋智乾坤启数高志远”, 横批“智启通义”,字体飘逸,中间挂在一着一副中国风的画作,内容是岳阳楼。","actual_prompt": "一副典雅庄重的对联悬挂于中式厅堂中央,对联左侧书写“义本生知人机同道善思新”,右侧书写“通云赋智乾坤启数高志远”,横批为“智启通义”,整体采用飘逸洒脱的书法字体,墨色浓淡相宜,展现出浓厚的传统韵味。对联中间悬挂一幅中国风画作,描绘的是著名的岳阳楼景观:楼阁飞檐翘角,依水而建,远处湖光潋滟,烟波浩渺,天空中有几缕轻云缭绕,营造出诗意盎然的意境。背景房间为安静古典的中式布置,木质家具线条流畅,桌上摆放着数件青花瓷器,纹饰精美,釉色莹润。整体空间光线柔和,营造出庄重、宁静的文化氛围。画面风格为传统中国水墨风,笔触细腻,层次分明,充满古典美感。","url": "https://dashscope-result-sz.oss-cn-shenzhen.aliyuncs.com/xxx.png?Expires=xxxx"}]},"usage": {"image_count": 1}
}
3、文生图Prompt指南
本文介绍文生图的提示词使用技巧,包含提示词公式、提示词词典,以帮助您快速上手图像创作,将创意转化为图像。
一、提示词参数
文生图模型有两个参数跟提示词有关:
- prompt:正向提示词,支持中英文。您需要用一段文字描述所需生成的图片。本文的提示词技巧指的是 prompt 的撰写技巧。
- negative_prompt:反向提示词,描述不希望在图像中看到的内容。
文生图V2还支持使用大模型智能改写prompt。
- prompt_extend:是否开启prompt智能改写。默认为true,即开启大模型智能改写。推荐您使用默认配置。
{"input": {"prompt": "一间有着精致窗户的花店,漂亮的木质门,摆放着花朵","negative_prompt": "人物"},"parameters": {"prompt_extend": true}
}
二、提示词公式
基础公式:


进阶公式:

