Wanxiang - Image Inpainting API Reference

Updated at:

This topic describes the input and output parameters of the Wanxiang - Image Inpainting model.

Related guide: Image Inpainting

Important

  • This document applies only to the China (Beijing) region. You must use an API key from this region.
  • The wanx-x-painting model is currently available for free trial only. After your free quota is exhausted, you cannot invoke the model because paid usage is not supported. We recommend using Image Editing - Qwen or Image Editing - Wanxiang 2.1 as alternatives.

ImportantAlibaba Cloud Model Studio provides a workspace-specific domain https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com for the China (Beijing) region, which delivers superior performance and higher stability for inference requests. We recommend migrating from https://dashscope.aliyuncs.com to the new domain.

{WorkspaceId} is your workspace ID, which you can view on the Workspace Details page in the Model Studio console. The existing domain remains available.

Model overview

Model preview

image

Model overview

Model

Model overview

wanx-x-painting

The Wan image inpainting model generates content that corresponds to the text description in the smeared area based on the original image, the local area smear map, and the text prompt that you enter. The area outside the smeared area remains basically unchanged.

Model description
ModelUnit priceRate limit (shared by Alibaba Cloud account and RAM users)

Free quota(View)

Request per second (RPS) for task submissionNumber of concurrent tasks

wanx-x-painting

For free trial only.

You cannot make calls after the free quota is used up. We recommend using Image Editing - Qwen or Image Editing - Wanxiang 2.1 as alternatives.

2

1

500 images

For more information, see Model billing and rate limiting.

Prerequisites

You can invoke the Image Inpainting API using HTTP or the DashScope SDK.

Before making a call, get an API key and export the API key as an environment variable.

To call the API using the SDK, install the DashScope SDK. The SDK is available for Python and Java.

HTTP call

Image models take a long time to process. To prevent timeouts, HTTP calls support only asynchronous result retrieval. Two requests are required:

  1. Create a task to obtain the task ID: First, initiate a task creation request, which returns a task ID (task_id).
  2. Query the result by task ID: Use the task ID obtained in the previous step to query the task status and result. When the task is executed successfully, the image URL is returned, which is valid for 24 hours.

NoteAfter creation, the task enters a queue for scheduling. Call the query API to retrieve the task status and result.

Step 1: Create a task to get a task ID

POST https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/aigc/image2image/image-synthesis

Request parameters

Request headers

Content-Type string (Required)

The content type of the request. Must be application/json.

Authorization string (Required)

Authenticates the request with a Model Studio API key. Example: Bearer sk-xxxx.

X-DashScope-Async string (Required)

Enables asynchronous processing. HTTP requests support only asynchronous calls. Must be set to enable.

ImportantIf this request header is missing, the error "current user api does not support synchronous calls" is returned.

Request body

modelstring(Required)

The name of the model. Example: wanx-x-painting.

inputobject(Required)

The basic input information, such as the prompt and image URLs.

Properties

promptstring(Required)

A positive prompt that describes the elements and visual characteristics for the generated image.

Chinese and English are supported. The maximum length is 75 characters. Excess characters are automatically truncated.

base_image_urlstring(Required)

The URL of the input image. Base64-encoded image data is not supported.

The URL must be publicly accessible and use HTTP or HTTPS.You can also obtain a temporary public URL.

The image must meet the following constraints:

  • Format: JPG, JPEG, PNG, or BMP.
  • Resolution: Must be greater than 256 × 256 pixels and less than 4096 × 4096 pixels.
  • File size: Must not exceed 10 MB.
  • The URL must not contain Chinese characters.

mask_image_urlstring(Required)

The image URL of the area marked by the user for inpainting. Its resolution must be consistent with that of the base_image_url image. Entering image Base64 data is not supported.

The URL must be publicly accessible and use HTTP or HTTPS.You can also obtain a temporary public URL.

The image must meet the following constraints:

  • Format: JPG, JPEG, PNG, or BMP.
  • Resolution: Must be greater than 256 × 256 pixels and less than 4096 × 4096 pixels.
  • File size: Must not exceed 10 MB.
  • The URL must not contain Chinese characters.

You can create a mask image by painting over the original image using Photoshop or other image editing tools.

parametersobject(Optional)

The parameters for image processing.

Properties

stylestring(Optional)

The style of the output image. The supported values are:

  • <auto>: The default style.
  • <3d cartoon>: 3D cartoon.
  • <anime>: Anime.
  • <oil painting>: Oil painting.
  • <watercolor>: Watercolor.
  • <sketch>: Sketch.
  • <chinese painting>: Chinese painting.
  • <flat illustration>: Flat illustration.

sizestring(Optional)

The resolution of the output image. The supported resolutions are:

  • 1024×1024: The default resolution.
  • 720×1280
  • 1280×720

ninteger(Optional)

The number of images to generate. The value must be an integer from 1 to 4. The default value is 1.

mask_colorarray(Optional)

A list of RGB color values that specify which colors in the mask image represent the area to be modified. The default value is [].

If this parameter is empty, the mask image is binarized, and white areas indicate the modification region. If this parameter is not empty, the specified RGB colors define the modification region. For example, [0,0,0] and [134,134,134]. Other colors are treated as the background.

Example: [[0, 0, 0], [134, 134, 134]].

Image inpainting

curl --location 'https://{WorkspaceId}.cn-beijing.maas.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": "一只狗戴着红色眼镜",
    "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
  }
}'

Response parameters

output object

The output information of the task.

Properties

task_id string

The task ID. Valid for queries for 24 hours.

task_status string

The status of the task.

Enumeration values

  • PENDING
  • RUNNING
  • SUCCEEDED
  • FAILED
  • CANCELED
  • UNKNOWN: The task does not exist or its status is unknown.

request_idstring

Unique request identifier for tracing and troubleshooting.

codestring

Error code. Returned only for failed requests. See Error codes.

messagestring

Detailed error message. Returned only for failed requests. See Error codes.

Successful response

Save the task_id, which is used to query the task status and result.

{
    "output": {
        "task_status": "PENDING",
        "task_id": "0385dc79-5ff8-4d82-bcb6-xxxxxx"
    },
    "request_id": "4909100c-7b5a-9f92-bfe5-xxxxxx"
}

Error response

Task creation failed. See Error codes.

{
    "code": "InvalidApiKey",
    "message": "No API-key provided.",
    "request_id": "7438d53d-6eb8-4596-8835-xxxxxx"
}

Step 2: Query results by task ID

GET https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/tasks/{task_id}

Request parameters

Headers

Authorization string (Required)

Authenticates the request with a Model Studio API key. Example: Bearer sk-xxxx.

Path parameters

task_id string (Required)

The ID of the task.

Query task results

Replace 86ecf553-d340-4e21-xxxxxxxxx with the actual task_id.

If you use a model in the Singapore region, replace base_url with https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/tasks/86ecf553-d340-4e21-xxxxxxxxx, where {WorkspaceId} is your actual workspace ID.

curl -X GET https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/tasks/86ecf553-d340-4e21-xxxxxxxxx \
--header "Authorization: Bearer $DASHSCOPE_API_KEY"

Response parameters

outputobject

The output information of the task.

Properties

task_id string

The task ID. Valid for queries for 24 hours.

task_status string

The status of the task.

Enumeration values

  • PENDING
  • RUNNING
  • SUCCEEDED
  • FAILED
  • CANCELED
  • UNKNOWN: The task does not exist or its status is unknown.

task_metrics object

Statistics for the task result.

Properties

TOTAL integer

The total number of tasks.

SUCCEEDED integer

The number of successful tasks.

FAILED integer

The number of failed tasks.

results array of object

The list of task results, including image URLs, error messages in case of partial task failure.

Structure

{
    "results": [
        {
            "url": ""
        },
        {
            "code": "",
            "message": ""
        }
    ]
}

codestring

Error code. Returned only for failed requests. See Error codes.

messagestring

Detailed error message. Returned only for failed requests. See Error codes.

usage object

The output statistics. Only successful results are counted.

Properties

image_count integer

Number of images successfully generated. Billing: Cost = Number of images × Unit price.

request_idstring

Unique request identifier for tracing and troubleshooting.

Success response

Task data (task status and image URLs) is retained for only 24 hours and then automatically purged. Save generated images promptly.

{
    "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
    }
}

Failure response

If the task fails to be executed, task_status is set to FAILED, and an error code and message are provided. See Error codes for troubleshooting.

{
    "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
        }
    }
}

Partial failure response

The model can generate multiple images per task. If at least one succeeds, the task status is SUCCEEDED and URLs of successful images are returned. Failed images include a failure reason. Usage statistics count only successful results. See Error codes.

{
    "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
    }
}

DashScope SDK invocation

Install the latest DashScope SDK. See Install the SDK.

The DashScope SDK supports Python and Java.

SDK parameter names are mostly consistent with the HTTP API. Parameter structure varies by SDK language. For details, see the HTTP Invocation section.

Because image models take a long time to process, the underlying service is asynchronous. The SDK supports both synchronous and asynchronous calls.

Python SDK invocation

Synchronous call

Request example
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
import os
import dashscope
from dashscope import DashScopeError

# 以下为华北2(北京)地域的配置,调用时请将{WorkspaceId}替换为真实的业务空间ID,各地域的region取值不同。
client = dashscope.Client(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    workspace="{WorkspaceId}",
    region="cn-beijing",
)

prompt = "一只狗戴着红色眼镜"
model = "wanx-x-painting"
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----')
try:
    job = client.images.generate(
        model=model,
        prompt=prompt,
        n=1,
        size='1024*1024',
        extra_input=extra_input)
    print(job)
    # save file to current directory
    for url in job.image_urls:
        file_name = PurePosixPath(unquote(urlparse(url).path)).parts[-1]
        with open('./%s' % file_name, 'wb+') as f:
            f.write(requests.get(url).content)
except DashScopeError as e:
    print(f"HTTP返回码:{e.status_code}")
    print(f"错误码:{e.code}")
    print(f"错误信息:{e.message}")
    print("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code")
Response example
{
	"request_id": "4126d9dd-e037-9f32-8d56-6d29ab3f9a06",
	"task_id": "b476bc4e-35c1-4c4e-a4d9-xxxxxxx",
	"status": "SUCCEEDED",
	"results": [{
		"url": "https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/xxxx.png"
	}],
	"task_metrics": {
		"TOTAL": 1,
		"SUCCEEDED": 1,
		"FAILED": 0
	},
	"usage": {
		"image_count": 1
	}
}

Asynchronous call

Request example
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
import os
import dashscope
from dashscope import DashScopeError

# 以下为华北2(北京)地域的配置,调用时请将{WorkspaceId}替换为真实的业务空间ID,各地域的region取值不同。
client = dashscope.Client(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    workspace="{WorkspaceId}",
    region="cn-beijing",
)

prompt = "一只狗戴着红色眼镜"
model = "wanx-x-painting"
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():
    job = client.images.jobs.create(
        model=model,
        prompt=prompt,
        n=1,
        size='1024*1024',
        extra_input=extra_input)
    print(job)
    print(job.task_id, job.status)
    return job

# 等待异步任务结束
def wait_async_task(job):
    done = client.images.jobs.wait(job)
    print(done)
    print(done.status)
    # save file to current directory
    for url in done.image_urls:
        file_name = PurePosixPath(unquote(urlparse(url).path)).parts[-1]
        with open('./%s' % file_name, 'wb+') as f:
            f.write(requests.get(url).content)

if __name__ == '__main__':
    try:
        async_call()
    except DashScopeError as e:
        print(f"HTTP返回码:{e.status_code}")
        print(f"错误码:{e.code}")
        print(f"错误信息:{e.message}")
        print("请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code")
Response example
1. Sample response for task creation
{
	"request_id": "31b04171-011c-96bd-ac00-f0383b669cc7",
	"task_id": "4f90cf14-a34e-4eae-xxxxxxxx",
	"status": "PENDING"
}
2. Sample response for querying task results
{
	"request_id": "d861d3ba-4b29-9491-abad-266ef4fb2f08",
	"task_id": "4f90cf14-a34e-4eae-xxxxxxxx",
	"status": "SUCCEEDED",
	"results": [{
		"url": "https://dashscope-result-hz.oss-cn-hangzhou.aliyuncs.com/xxxx.png"
	}],
	"task_metrics": {
		"TOTAL": 1,
		"SUCCEEDED": 1,
		"FAILED": 0
	},
	"usage": {
		"image_count": 1
	}
}

Java SDK invocation

Synchronous call

Request example
// Copyright (c) Alibaba, Inc. and its affiliates.

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;
import com.alibaba.dashscope.utils.Constants;

public class Main {
    static {Constants.baseHttpApiUrl="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1";}

    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 = "一只狗戴着红色眼镜";
        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();
    }

}
Response example
{
	"request_id": "150edcda-05d5-9ffe-8803-84626d1db623",
	"output": {
		"task_id": "f2098ff0-146e-404c-bb25-xxxxxxxx",
		"task_status": "SUCCEEDED",
		"results": [{
			"url": "https://dashscope-result-hz.oss-cn-hangzhou.aliyuncs.com/xxxx.png"
		}],
		"task_metrics": {
			"TOTAL": 1,
			"SUCCEEDED": 1,
			"FAILED": 0
		}
	},
	"usage": {
		"image_count": 1
	}
}

Asynchronous call

Request example
// Copyright (c) Alibaba, Inc. and its affiliates.

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;
import com.alibaba.dashscope.utils.Constants;

public class Main {
    static {Constants.baseHttpApiUrl="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1";}

    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 = "一只狗戴着红色眼镜";
        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();
    }

}
Response example
1. Sample response for task creation
{
	"request_id": "5dbf9dc5-4f4c-9605-85ea-542f97709ba8",
	"output": {
		"task_id": "7277e20e-aa01-4709-xxxxxxxx",
		"task_status": "PENDING"
	}
}
2. Sample response for querying task resultsContent of the output field:
{
	"task_id": "7277e20e-aa01-4709-xxxxxxxx",
	"task_status": "SUCCEEDED",
	"results": [{
		"url": "https://dashscope-result-hz.oss-cn-hangzhou.aliyuncs.com/xxxx.png"
	}],
	"task_metrics": {
		"TOTAL": 1,
		"SUCCEEDED": 1,
		"FAILED": 0
	}
}
Content of the usage field:
{
	"image_count": 1
}

Error codes

If the model call fails and returns an error message, see Error codes for resolution.

FAQ

Model Billing and Rate Limiting

Free quota
  • Description: The free quota applies only to successfully generated output images. Input images or failed model processing do not consume the free quota.
  • How to get: It is automatically granted when you activate Model Studio. Valid for 90 days.
  • Account scope: Shared between your Alibaba Cloud account and its RAM users.
  • For more details, see Free quota for new users.
Limited-time free trial
  • If billing shows a limited-time free trial, the model is in public preview. You cannot use the model after the free quota runs out.
Billing details
  • If a clear unit price is shown — for example, CNY 0.2 per second — the model is commercially available. You must pay after the free quota expires or is fully consumed.
  • Billing scope: Charges apply only to successfully generated output images. Other scenarios are not billed.
  • Billing method: Billing is applied to your Alibaba Cloud account. RAM users cannot be billed independently. Your Alibaba Cloud account must cover all associated charges. To view billing information, go to the Bills page.
  • Recharge: Go to the Expenses and Costs console.
  • View invocation metrics: Go to the Monitoring page.
  • For more billing questions, see Billing Items.
Rate limiting
  • Rate limit scope: Shared between your Alibaba Cloud account and its RAM users.

Common Issues with Image Inpainting API

  1. What are the restrictions on the mask image (mask_image) of the input image?
    • Only image URLs are supported. Base64-encoded image data is not supported.
    • The mask image must have the same resolution as the input image.
    • The sample mask image in this document has a white background. However, the mask image is not required to have a white background. You can specify other colors using the mask_color field.
  2. How can I obtain the mask image for the input image?

    You can use Photoshop or other image editing tools to draw on the original image to obtain the mask image.

  3. When I use the Image Inpainting feature, what should I do if the quality of the output image outside the painted area is not ideal?

    You can use the inpainting feature of General Image Editing to regenerate the image.