AI 扩图 V2
描述
采用 AI 图像扩展技术,根据原图内容生成边界外的画面。支持按整体比例、按四边比例或像素数扩展,也可按目标宽高比自动扩展。
版本
1.0
图片要求
支持 JPG、PNG。
调用 URL
正式环境:https://openapi.meitu.com
任务提交接口:https://openapi.meitu.com/api/v1/sdk/sync/push
任务名称(task):/v1/AI_Image_Expansion_v2/497076
任务类型(task_type):formula调用方法
POST
Content-Type: application/json
权限
使用 开放平台接口签名。
请求参数
| 是否必选 | 参数名 | 类型 | 参数说明 |
|---|---|---|---|
| 是 | params | string | 算法参数(JSON 字符串)。 |
| 是 | init_images | object[] | 输入图片列表,传一项。 |
| 是 | task | string | /v1/AI_Image_Expansion_v2/497076 |
| 是 | task_type | string | formula |
| 否 | sync_timeout | integer | 传 -1 时提交任务后不等待结果。 |
init_images
| 是否必选 | 参数名 | 类型 | 参数说明 |
|---|---|---|---|
| 是 | url | string | 输入图片 URL。 |
| — | profile | object | 图片的媒体属性。 |
profile
| 是否必选 | 参数名 | 类型 | 参数说明 |
|---|---|---|---|
| — | media_profiles | object | 媒体类型配置。 |
| — | version | string | 示例值:v1。 |
media_profiles
| 是否必选 | 参数名 | 类型 | 参数说明 |
|---|---|---|---|
| — | media_data_type | string | url 表示使用图片 URL。 |
params
解码 params 后,算法参数位于 parameter 对象中。
| 是否必选 | 参数名 | 类型 | 参数说明 |
|---|---|---|---|
| 是 | parameter | object | 扩图参数。 |
parameter
| 是否必选 | 参数名 | 类型 | 参数说明 |
|---|---|---|---|
| 否 | expand_ratio | number | 整体扩展比例,范围 [0, 1],默认 0.5。0.1 表示宽、高各扩至原尺寸的 120%;0.5 表示各扩至 200%。 |
| 否 | free_expand_ratio | object | 按四边比例扩展。各值范围为 [0, 1]。 |
| 否 | free_expand_pixel | object | 按四边像素数扩展。各值为非负整数。 |
| 否 | mode | integer | 不传时使用手动扩展参数;1 按 ratio 自动扩展;2 自动扩展为 1:1,无需传 ratio。 |
| 条件必选 | ratio | string | mode=1 时必传。取值为 arbitrary、1:1、3:4、4:3、9:16 或 16:9。 |
| 否 | generate_num | integer | 生成图片数量,默认 1,最大 20。 |
| 否 | seed | integer | 随机种子,范围 [-1, 65535],默认 0。固定种子便于复现;-1 表示随机。 |
| 否 | extra_prompt | string | 生成内容的附加提示词。使用英文,多条提示词用逗号分隔。 |
| 否 | high_quality_encode | boolean | 是否以较低压缩率编码输出图像,默认 false。开启后画质更高、耗时可能增加,不改变生成内容。 |
| 否 | rsp_media_type | string | url 返回结果图片 URL;jpg 返回 Base64 图片数据。 |
手动扩展时,expand_ratio、free_expand_ratio 和 free_expand_pixel 最多传一个。使用 free_expand_ratio 或 free_expand_pixel 时,须同时提供 top、bottom、left、right;不扩展的方向填 0。比例模式中,上下以原图高度为基准,左右以原图宽度为基准。例如,top: 0.1 表示向上增加原图高度的 10%。
输入值示例
以下示例在四边各扩展原尺寸的 25%。
{
"task": "/v1/AI_Image_Expansion_v2/497076",
"task_type": "formula",
"init_images": [
{
"url": "https://example.com/input.jpg",
"profile": {
"media_profiles": {
"media_data_type": "url"
},
"version": "v1"
}
}
],
"params": "{\"parameter\":{\"rsp_media_type\":\"url\",\"free_expand_ratio\":{\"top\":0.25,\"bottom\":0.25,\"left\":0.25,\"right\":0.25},\"seed\":42,\"extra_prompt\":\"Extend the background and keep its color consistent.\"}}",
"sync_timeout": -1
}返回值说明
| 字段 | 类型 | 说明 |
|---|---|---|
error_code | integer | 错误码,0 表示成功。 |
message | string | 处理信息。 |
data.status | integer | 任务状态。 |
data.result.id | string | 任务 ID,用于查询任务。 |
data.result.urls | string[] | 任务成功后返回的结果图片 URL。 |
data.progress | number | 任务进度,1 表示已完成。 |
返回值示例
请求成功返回示例
Response Status: 200
Content-Type: application/json; charset=utf-8
{
"error_code": 0,
"message": "success",
"data": {
"status": 10,
"result": {
"id": "example-task-id",
"urls": [
"https://example.com/result.jpeg"
]
},
"progress": 1
}
}需要查询时
当 data.status = 9 时,同步等待已超时,任务不一定失败。使用 data.result.id 作为任务 ID,通过任务查询接口查询结果。
查询返回 0 或 1 时任务尚未完成,10 为成功,2 为失败。
请求失败返回示例
Response Status: 400
Content-Type: application/json; charset=utf-8
通用的错误代码与信息
详见 API 错误码。
SDK 调用示例
调用前填写 AK/SK 和图片 URL。
Python
import requests
from sign_sdk import sign
url = "https://openapi.meitu.com/api/v1/sdk/sync/push"
body = r'''{
"task": "/v1/AI_Image_Expansion_v2/497076",
"task_type": "formula",
"init_images": [
{
"url": "https://example.com/input.jpg",
"profile": {
"media_profiles": {
"media_data_type": "url"
},
"version": "v1"
}
}
],
"params": "{\"parameter\":{\"rsp_media_type\":\"url\",\"free_expand_ratio\":{\"top\":0.25,\"bottom\":0.25,\"left\":0.25,\"right\":0.25},\"seed\":42,\"extra_prompt\":\"Extend the background and keep its color consistent.\"}}",
"sync_timeout": -1
}'''
headers = {"Content-Type": "application/json", sign.HeaderHost: "openapi.meitu.com"}
signer = sign.Signer("YOUR_ACCESS_KEY", "YOUR_SECRET_KEY")
request = signer.sign(url, "POST", headers, body)
with requests.Session() as session:
response = session.send(request, timeout=(10, 60), allow_redirects=False)
print(response.status_code, response.text)Go
package main
import (
"fmt"
"io"
"log"
"net/http"
"time"
"github.com/mtlab/api/signer"
)
func main() {
url := "https://openapi.meitu.com/api/v1/sdk/sync/push"
body := `{
"task": "/v1/AI_Image_Expansion_v2/497076",
"task_type": "formula",
"init_images": [
{
"url": "https://example.com/input.jpg",
"profile": {
"media_profiles": {
"media_data_type": "url"
},
"version": "v1"
}
}
],
"params": "{\"parameter\":{\"rsp_media_type\":\"url\",\"free_expand_ratio\":{\"top\":0.25,\"bottom\":0.25,\"left\":0.25,\"right\":0.25},\"seed\":42,\"extra_prompt\":\"Extend the background and keep its color consistent.\"}}",
"sync_timeout": -1
}`
headers := make(http.Header)
headers.Set(signer.HeaderHost, "openapi.meitu.com")
headers.Set("Content-Type", "application/json")
s := signer.NewSigner("YOUR_ACCESS_KEY", "YOUR_SECRET_KEY")
req, err := s.Sign(url, http.MethodPost, headers, body)
if err != nil {
log.Fatal(err)
}
client := &http.Client{
Timeout: 60 * time.Second,
CheckRedirect: func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
},
}
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
result, err := io.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(res.StatusCode, string(result))
}PHP
<?php
require_once __DIR__ . '/signer.php';
$url = 'https://openapi.meitu.com/api/v1/sdk/sync/push';
$body = <<<'JSON'
{
"task": "/v1/AI_Image_Expansion_v2/497076",
"task_type": "formula",
"init_images": [
{
"url": "https://example.com/input.jpg",
"profile": {
"media_profiles": {
"media_data_type": "url"
},
"version": "v1"
}
}
],
"params": "{\"parameter\":{\"rsp_media_type\":\"url\",\"free_expand_ratio\":{\"top\":0.25,\"bottom\":0.25,\"left\":0.25,\"right\":0.25},\"seed\":42,\"extra_prompt\":\"Extend the background and keep its color consistent.\"}}",
"sync_timeout": -1
}
JSON;
$headers = ['Host' => 'openapi.meitu.com', 'Content-Type' => 'application/json'];
$signer = new Signer('YOUR_ACCESS_KEY', 'YOUR_SECRET_KEY');
$curl = $signer->sign($url, 'POST', $headers, $body);
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 60,
]);
$result = curl_exec($curl);
if ($result === false) {
$error = curl_error($curl);
curl_close($curl);
throw new RuntimeException($error);
}
echo curl_getinfo($curl, CURLINFO_HTTP_CODE), PHP_EOL, $result, PHP_EOL;
curl_close($curl);Java
package com.meitu.openai.common;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) throws Exception {
String url = "https://openapi.meitu.com/api/v1/sdk/sync/push";
String body = "{\n" +
" \"task\": \"/v1/AI_Image_Expansion_v2/497076\",\n" +
" \"task_type\": \"formula\",\n" +
" \"init_images\": [\n" +
" {\n" +
" \"url\": \"https://example.com/input.jpg\",\n" +
" \"profile\": {\n" +
" \"media_profiles\": {\n" +
" \"media_data_type\": \"url\"\n" +
" },\n" +
" \"version\": \"v1\"\n" +
" }\n" +
" }\n" +
" ],\n" +
" \"params\": \"{\\\"parameter\\\":{\\\"rsp_media_type\\\":\\\"url\\\",\\\"free_expand_ratio\\\":{\\\"top\\\":0.25,\\\"bottom\\\":0.25,\\\"left\\\":0.25,\\\"right\\\":0.25},\\\"seed\\\":42,\\\"extra_prompt\\\":\\\"Extend the background and keep its color consistent.\\\"}}\",\n" +
" \"sync_timeout\": -1\n" +
"}";
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put(Signer.HeaderHost, "openapi.meitu.com");
Signer signer = new Signer("YOUR_ACCESS_KEY", "YOUR_SECRET_KEY");
Map<String, String> signed = signer.sign(url, "POST", headers, body);
HttpURLConnection connection = (HttpURLConnection) URI.create(url).toURL().openConnection();
try {
connection.setRequestMethod("POST");
connection.setConnectTimeout(10000);
connection.setReadTimeout(60000);
connection.setInstanceFollowRedirects(false);
signed.forEach(connection::setRequestProperty);
connection.setDoOutput(true);
try (OutputStream out = connection.getOutputStream()) {
out.write(body.getBytes(StandardCharsets.UTF_8));
}
int status = connection.getResponseCode();
try (InputStream in = status >= 400 ? connection.getErrorStream() : connection.getInputStream()) {
String result = in == null ? "" : new String(in.readAllBytes(), StandardCharsets.UTF_8);
System.out.println(status + " " + result);
}
} finally {
connection.disconnect();
}
}
}