文档

图像局部重绘API参考

更新时间:

文档简介

本文介绍图像局部重绘模型的输入输出参数。图像局部重绘是根据用户提供原始图片和局部涂抹图片,再加上文字描述,然后在涂抹区域生成文字描述的内容,涂抹区域外没有变化。

相关指南图像局部重绘

模型概览

模型名

模型简介

免费额度

计费单价

限流(含主账号与RAM子账号)

任务下发接口QPS限制

同时处理中任务数量

wanx-x-painting

通义万相-图像局部重绘是基于自研的Composer组合生成框架的AI绘画创作大模型后置处理链路,能够根据用户输入的原始图片、随意涂抹图中局部区域和prompt提示词文字内容,生成符合语义描述的多样化风格的局部重绘图像。通过知识重组与可变维度扩散模型,加速收敛并提升最终生成图片的效果, 布局自然、细节丰富、画面细腻、结果逼真。

免费额度:500张

领取方式:开通阿里云百炼大模型服务后,自动发放

有效期:180天

限时免费

2

1

前提条件

您需要已获取API-KEY配置API-KEY到环境变量。如果通过SDK调用,还需要安装DashScope SDK

HTTP调用

为了减少等待时间并且避免请求超时,服务采用异步方式提供。您需要发起两个请求:

  • 创建任务:首先发送一个请求创建文生图任务,该请求会返回任务ID。

  • 根据任务ID查询结果:使用上一步获得的任务ID,查询模型生成的结果。

创建任务

POST https://dashscope.aliyuncs.com/api/v1/services/aigc/image2image/image-synthesis

请求头(Headers)

curl

curl --location 'https://dashscope.aliyuncs.com/api/v1/services/aigc/image2image/image-synthesis' \
--header 'X-DashScope-Async: enable' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
  "model": "wanx-x-painting",
  "input": {
    "prompt": "a dog wearing red glasses",
    "base_image_url": "http://synthesis-source.oss-accelerate.aliyuncs.com/lingji/validation/mask2img/demo/source3.jpg",
    "mask_image_url": "http://synthesis-source.oss-accelerate.aliyuncs.com/lingji/validation/mask2img/demo/glasses.png"
  },
  "parameters": {
    "size": "1024*1024",
    "n": 1
  }
}'

Content-Type string 必选

请求内容类型。固定为application/json

Authorization string 必选

推荐您使用百炼API-Key,也可填DashScope API-Key。例如:Bearer d1xxx2a。

X-DashScope-Async string 必选

是否开启异步处理。必须开启异步处理,设置为enable

请求体(Request Body)

model string 必选

调用模型。

input object 必选

输入图像的基本信息,比如图像URL地址。

属性

prompt string 必选

正向提示词,描述生成图像中期望包含的元素和视觉特点,通过对正向提示词的细节描述,可以使图片更接近自己的预期效果。

支持中英文,长度不超过75个字符,超过部分会自动截断。

base_image_url string 必选

输入待修改的图像URL地址。

图像限制:

  • 图像限制;

  • 图像格式:JPG、JPEG、PNG、BMP。

  • 图像大小:不超过10MB。

  • 图像分辨率:大于256×256像素,小于4096×4096像素。

  • URL地址中不能包含中文字符。

mask_image_url string 必选

用户标记涂抹区域的图像URL地址,需要和base_image_url图像分辨率保持一致。

图像限制;

  • 图像格式:JPG、JPEG、PNG、BMP。

  • 图像大小:不超过10MB。

  • 图像分辨率:大于256×256像素,小于4096×4096像素。

  • URL地址中不能包含中文字符。

parameters object 可选

图像处理参数。

属性

style string 可选

涂抹修改区域的风格。目前支持以下风格取值:

  • <auto>: 默认。

  • <3d cartoon>:3D卡通。

  • <anime>:动画。

  • <oil painting>: 油画。

  • <watercolor>:水彩。

  • <sketch>:素描。

  • <chinese painting>:中国画。

  • <flat illustration>:扁平插画。

size string 可选

生成图像的分辨率,目前仅支持1024×1024、720×1280、1280×720三种分辨率,默认为1024×1024像素。

n integer 可选

生成的图片数量,目前支持1~4张,默认为1。

mask_color list 可选

掩码图片中表示涂改区域的可选RGB颜色数值。默认值为[]。

当该字段为空,默认对mask进行二值化处理(处理后白色代表涂抹区域);当该字段非空,任一向量中的RGB三通道数值(例如[0,0,0]和[134,134,134])均可代表涂抹区域,不在向量中数值均为背景色。

响应

成功响应

{
    "output": {
        "task_id": "13b1848b-5493-4c0e-8c44-68d038b492af",
        "task_status": "PENDING"
    },
    "request_id": "7574ee8f-38a3-4b1e-9280-11c33ab46e51"
}

异常响应

{
    "code":"InvalidApiKey",
    "message":"Invalid API-key provided.",
    "request_id":"fb53c4ec-1c12-4fc4-a580-cdb7c3261fc1"
}

output dict

任务输出信息。

属性

task_id string

任务id。

task_status string

任务状态。

  • PENDING:排队中

  • RUNNING:处理中

  • SUSPENDED:挂起

  • SUCCEEDED:执行成功

  • FAILED:执行失败

  • UNKNOWN:任务不存在或状态未知

code string

请求失败,表示错误码,成功时返回参数中不会包含该参数。

message string

请求失败,表示失败详细信息,成功时返回参数中不会包含该参数。

request_id string

请求唯一标识。可用于请求明细溯源和问题排查。

根据任务ID查询结果

GET https://dashscope.aliyuncs.com/api/v1/tasks/{task_id}

请求头(Headers)

获取任务结果

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

Authorization string 必选

推荐使用百炼API-Key,也可填DashScope API-Key。例如:Bearer d1xxx2a。

URL路径参数(Path parameters)

task_id string 必选

任务id。

响应

任务执行成功

对于本模型,任务状态和结果将保留24小时,生成图像的URL地址有效期也为24小时,需要您在有效期内及时转存生成图像,24小时之后,任务连同结果一起从系统中清除。

{
    "request_id":"85eaba38-0185-99d7-8d16-4d9135238846",
    "output":{
        "task_id":"86ecf553-d340-4e21-af6e-a0c6a421c010",
        "task_status":"SUCCEEDED",
        "results":[
            {
                "url":"https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/123/a1.png
            },
            {
                "url":"https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/123/b2.png
            }
        ],
        "task_metrics":{
            "TOTAL":2,
            "SUCCEEDED":2,
            "FAILED":0
        }
    },
    "usage":{
        "image_count":2
    }
}

任务执行中

{
    "request_id":"e5d70b02-ebd3-98ce-9fe8-759d7d7b107d",
    "output":{
        "task_id":"86ecf553-d340-4e21-af6e-a0c6a421c010",
        "task_status":"RUNNING",
        "task_metrics":{
            "TOTAL":4,
            "SUCCEEDED":1,
            "FAILED":0
        }
    }
}

任务执行失败

{
    "request_id":"e5d70b02-ebd3-98ce-9fe8-759d7d7b107d",
    "output":{
        "task_id":"86ecf553-d340-4e21-af6e-a0c6a421c010",
        "task_status":"FAILED",
        "code":"InvalidParameter"
	"message":"The size is not match the allowed size ['1024*1024', '720*1280', '1280*720']"
        "task_metrics":{
            "TOTAL":4,
            "SUCCEEDED":0,
            "FAILED":4
        }
    }
}

任务部分失败

    "request_id":"85eaba38-0185-99d7-8d16-4d9135238846",
    "output":{
        "task_id":"86ecf553-d340-4e21-af6e-a0c6a421c010",
        "task_status":"SUCCEEDED",
        "results":[
            {
                "url":"https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/123/a1.png
            },
            {
              	"code": "InternalError.Timeout"
								"message": "An internal timeout error has occured during execution, please try again later or contact service support."
            }
        ],
        "task_metrics":{
            "TOTAL":2,
            "SUCCEEDED":1,
            "FAILED":1
        }
    },
    "usage":{
        "image_count":1
    }
}

output object

调用结果信息,对于千问模型,包含输出text。

属性

task_id string

任务id。

task_status string

任务状态:

  • PENDING:排队中

  • RUNNING:处理中

  • SUSPENDED:挂起

  • SUCCEEDED:执行成功

  • FAILED:执行失败

  • UNKNOWN:任务不存在或状态未知

task_metrics object

每个任务结果统计。

属性

TOTAL integer

总的任务数。

SUCCEEDED integer

任务状态为成功的任务数。

FAILED integer

任务状态为失败的任务数。

code string

接口错误码。接口成功请求不会返回该参数。

message string

接口错误信息。接口成功请求不会返回该参数。

results list

图像结果列表。

request_id string

请求唯一标识。可用于请求明细溯源和问题排查。

usage object

输出信息统计。

属性

image_count integer

本次请求成功生成的图片数量。

SDK调用

请先确认已安装最新版SDK:安装SDK

此API支持Python SDK和Java SDK。SDK接口的入参和出参可参考HTTP调用

由于本模型请求处理时间较长,服务采用异步方式提供,SDK进行了封装。您既可以按异步方式调用,也可以按照同步方式调用。

Python

同步调用

from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
from dashscope import ImageSynthesis

prompt = "a dog wearing red glasses"
model = "wanx-x-painting"
task = "image2image"
extra_input = {
    "base_image_url": "http://synthesis-source.oss-accelerate.aliyuncs.com/lingji/validation/mask2img/demo/source3.jpg",
    "mask_image_url": "http://synthesis-source.oss-accelerate.aliyuncs.com/lingji/validation/mask2img/demo/glasses.png"
}


print('----sync call, please wait a moment----')
rsp = ImageSynthesis.call(model=model,
                          prompt=prompt,
                          n=1,
                          size='1024*1024',
                          task=task,
                          extra_input=extra_input)
if rsp.status_code == HTTPStatus.OK:
    print(rsp.output)
    # save file to current directory
    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))

异步调用

from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
from dashscope import ImageSynthesis

prompt = "a dog wearing red glasses"
model = "wanx-x-painting"
task = "image2image"
extra_input = {
    "base_image_url": "http://synthesis-source.oss-accelerate.aliyuncs.com/lingji/validation/mask2img/demo/source3.jpg",
    "mask_image_url": "http://synthesis-source.oss-accelerate.aliyuncs.com/lingji/validation/mask2img/demo/glasses.png"
}


def async_call():
    print('----create task----')
    task_info = create_async_task()
    print('----wait task done then save image----')
    wait_async_task(task_info)


# 创建异步任务
def create_async_task():
    rsp = ImageSynthesis.async_call(model=model,
                                    prompt=prompt,
                                    n=1,
                                    size='1024*1024',
                                    task=task,
                                    extra_input=extra_input)
    print(rsp)
    if rsp.status_code == HTTPStatus.OK:
        print(rsp.output)
    else:
        print('create_async_task Failed, status_code: %s, code: %s, message: %s' %
              (rsp.status_code, rsp.code, rsp.message))
    return rsp


# 等待异步任务结束
def wait_async_task(task):
    rsp = ImageSynthesis.wait(task)
    print(rsp)
    if rsp.status_code == HTTPStatus.OK:
        print(rsp.output.task_status)
        # save file to current directory
        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('Failed, status_code: %s, code: %s, message: %s' %
              (rsp.status_code, rsp.code, rsp.message))


if __name__ == '__main__':
    async_call()

Java

同步调用

import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
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.utils.JsonUtils;

import java.util.HashMap;

public class Main {

    public void syncCall() {
        String task = "image2image";
        ImageSynthesis imageSynthesis = new ImageSynthesis(task);
        ImageSynthesisParam param = genImageSynthesis();
        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));
    }

    private ImageSynthesisParam genImageSynthesis(){
        HashMap<String,Object> extraInputMap = new HashMap<>();
        extraInputMap.put("base_image_url", "http://synthesis-source.oss-accelerate.aliyuncs.com/lingji/validation/mask2img/demo/source3.jpg");
        extraInputMap.put("mask_image_url", "http://synthesis-source.oss-accelerate.aliyuncs.com/lingji/validation/mask2img/demo/glasses.png");
        String prompt = "a dog wearing red glasses";
        String model = "wanx-x-painting";
        return ImageSynthesisParam.builder()
                .model(model)
                .prompt(prompt)
                .n(1)
                .size("1024*1024")
                .extraInputs(extraInputMap)
                .build();
    }


    public static void main(String[] args){
        Main text2Image = new Main();
        text2Image.syncCall();
    }

}

异步调用

import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
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.utils.JsonUtils;

import java.util.HashMap;

public class Main {

    public void asyncCall() {
        System.out.println("---create task----");
        String taskId = this.createAsyncTask();
        System.out.println("---wait task done then return image url----");
        this.waitAsyncTask(taskId);
    }


    /**
     * 创建异步任务
     * @return taskId
     */
    public String createAsyncTask() {
        String task = "image2image";
        ImageSynthesis imageSynthesis = new ImageSynthesis(task);
        ImageSynthesisParam param = genImageSynthesis();
        ImageSynthesisResult result = null;
        try {
            result = imageSynthesis.asyncCall(param);
        } catch (ApiException | NoApiKeyException e){
            throw new RuntimeException(e.getMessage());
        }
        String taskId = result.getOutput().getTaskId();
        System.out.println("taskId=" + taskId);
        return taskId;
    }


    private ImageSynthesisParam genImageSynthesis(){
        String prompt = "a dog wearing red glasses";
        String model = "wanx-x-painting";
        HashMap<String,Object> extraInputMap = new HashMap<>();
        extraInputMap.put("base_image_url", "http://synthesis-source.oss-accelerate.aliyuncs.com/lingji/validation/mask2img/demo/source3.jpg");
        extraInputMap.put("mask_image_url", "http://synthesis-source.oss-accelerate.aliyuncs.com/lingji/validation/mask2img/demo/glasses.png");

        return ImageSynthesisParam.builder()
                .model(model)
                .prompt(prompt)
                .n(1)
                .size("1024*1024")
                .extraInputs(extraInputMap)
                .build();
    }


    /**
     * 等待异步任务结束
     * @param taskId 任务id
     * */
    public void waitAsyncTask(String taskId) {
        ImageSynthesis imageSynthesis = new ImageSynthesis();
        ImageSynthesisResult result = null;
        try {
            // If you have set the DASHSCOPE_API_KEY in the system environment variable, the apiKey can be null.
            result = imageSynthesis.wait(taskId, null);
        } catch (ApiException | NoApiKeyException e){
            throw new RuntimeException(e.getMessage());
        }

        System.out.println(JsonUtils.toJson(result.getOutput()));
        System.out.println(JsonUtils.toJson(result.getUsage()));
    }


    public static void main(String[] args){
        Main text2Image = new Main();
        text2Image.asyncCall();
    }

}

错误码

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