图像风格化
描述
本接口用于按照指定配方模板对输入图片进行风格化处理。配方 ID 属于动态业务配置,需联系商务获取后替换到 task 路径中。
版本
1.0
图片要求
- 支持 JPG、PNG 格式。
调用 URL
正式环境:https://openapi.meitu.com
任务提交接口:https://openapi.meitu.com/api/v1/sdk/sync/push
任务名称(task):/v1/image_style/default/{formula_id}
任务类型(task_type):formula{formula_id} 是配方 ID 占位符,必须替换为商务提供的实际配方 ID。本文不提供可直接调用的配方 ID;未替换占位符的示例不能用于线上请求。
调用方法
POST
Content-Type: application/json
权限
请求参数
| 是否必选 | 参数名 | 类型 | 参数说明 |
|---|---|---|---|
| 必选 | params | string | 算法参数(JSON 字符串) |
| 必选 | init_images | object[] | 图片文件列表 |
| 必选 | task | string | 动态路径 /v1/image_style/default/{formula_id};将 {formula_id} 替换为商务提供的配方 ID |
| 必选 | task_type | string | 固定 formula |
| 可选 | sync_timeout | int | 默认 30;-1 表示不等待。同步等待超时后,返回状态 9,需使用查询接口获取结果 |
init_images 多媒体文件参数,结构说明如下
| 是否必选 | 参数名 | 类型 | 参数说明 |
|---|---|---|---|
| 必选 | url | string | 图片 URL 或 base64 |
| 必选 | profile | object | 属性信息描述 |
profile 属性信息描述
| 是否必选 | 参数名 | 类型 | 参数说明 |
|---|---|---|---|
| 必选 | media_profiles | object | 媒体属性信息 |
| 必选 | version | string | 固定 v1 |
media_profiles 媒体属性信息
| 是否必选 | 参数名 | 类型 | 参数说明 |
|---|---|---|---|
| 必选 | media_data_type | string | url 表示 URL;jpg 表示 JPG 图片的 base64 |
params 推理参数是 JSON 字符串,其结构说明如下
| 是否必选 | 字段 | 类型 | 说明 |
|---|---|---|---|
| 可选 | rsp_media_type | string | 默认 url;jpg 表示结果图片以 base64 返回,url 表示结果图片以 URL 返回 |
| 必选 | parameter | object | 算法核心参数对象 |
parameter 算法参数详情
| 是否必选 | 参数名 | 类型 | 参数说明 |
|---|---|---|---|
| 可选 | prompt | string | 正向提示词;可用 (prompt:weight) 控制文本权重,使用 <lora:model:weight> 配置 LoRA 模型及权重。可用风格模型通过相关接口获取。示例:girl,(cartoon:1.1),<lora:074594b2d60230282482d176d28c7c2e_1.0:0.8> |
| 可选 | seed | int | 随机数种子,默认 -1;取正数时生成内容相对稳定,-1 时具有随机差异 |
| 可选 | width | int | 宽度,默认 1024;可用尺寸通过相关接口获取 |
| 可选 | height | int | 高度,默认 1024;可用尺寸通过相关接口获取 |
| 可选 | resize_mode | int | 画面缩放方式:1 自动缩放,2 拉伸,3 裁剪;默认裁剪 |
| 可选 | negative_prompt | string | 负向提示词 |
| 可选 | enable_image_face_detect | bool | 是否启用人脸检测拦截 |
| 可选 | steps | int | 采样步骤,默认 50 |
| 可选 | denoising_strength | float64 | 重绘幅度,范围 [0,1],步进 0.01,默认 0.8 |
| 可选 | cfg_scale | float64 | 提示词相关性,范围 [1,20],步进 0.5,默认 5 |
| 可选 | face_generation_scale | float64 | 人脸相似度,范围 0~1.0,默认 0.3;传负值(如 -1)关闭 |
| 可选 | batch_size | int | 生成图片批次,默认 1 |
| 可选 | sample_num | int | 生成图片数量,默认 4,范围 1~4 |
| 可选 | adetailer_face | int | 面部修复插件:1 开启,2 关闭;默认开启 |
输入值示例
以下示例中的 {formula_id} 必须替换为商务提供的实际配方 ID。
{
"task": "/v1/image_style/default/{formula_id}",
"task_type": "formula",
"init_images": [
{
"url": "https://example.com/input.jpg",
"profile": {
"media_profiles": {
"media_data_type": "url"
},
"version": "v1"
}
}
],
"params": "{\"rsp_media_type\":\"url\",\"parameter\":{\"prompt\":\"girl,(cartoon:1.1)\",\"seed\":-1,\"width\":1024,\"height\":1024,\"resize_mode\":3,\"negative_prompt\":\"\",\"enable_image_face_detect\":false,\"steps\":50,\"denoising_strength\":0.8,\"cfg_scale\":5,\"face_generation_scale\":0.3,\"batch_size\":1,\"sample_num\":4,\"adetailer_face\":1}}",
"sync_timeout": 30
}返回值说明
注意,生成的结果图会定期清理,请及时下载保存| 字段 | 类型 | 说明 |
|---|---|---|
| request_id | string | 请求标识 |
| trace_id | string | 链路追踪标识 |
| code | int | 业务状态码,0 表示请求受理成功 |
| error_code | int | 错误码,成功时为 0 |
| message | string | 业务信息 |
| tips | any | 补充提示,可为 null |
| data | object | 任务状态和算法结果 |
data 字段说明
| 字段 | 类型 | 说明 |
|---|---|---|
| status | int | 状态码:-1 未找到任务;0 创建成功;1 执行中;2 失败;9 超时,使用查询接口查询;10 成功 |
| result | object | 算法结果 |
| progress | number | 任务进度,例如 0.1、0.85、1 |
| predict_elapsed | int | 预计耗时,毫秒 |
| create_time | int64 | 创建时间戳,毫秒 |
| task_id | string | 任务 ID |
| custom_task_id | string | 客户自定义任务 ID |
| trace_id | string | 链路追踪标识 |
| client_info | string | 客户端信息 |
| init_images | object[]/null | 输入媒体回显 |
result 字段说明
| 字段 | 类型 | 说明 |
|---|---|---|
| id | string | 任务 ID |
| urls | string[] | 结果图片地址列表 |
返回值示例
请求成功返回示例
{
"request_id": "req_1234567890",
"trace_id": "trace_1234567890",
"code": 0,
"error_code": 0,
"message": "success",
"tips": null,
"data": {
"status": 10,
"result": {
"id": "50309bd5-a827-4125-bc96-62039c93770b",
"urls": ["https://example.com/result.png"]
},
"progress": 1,
"predict_elapsed": 0,
"create_time": 1718172000000,
"task_id": "50309bd5-a827-4125-bc96-62039c93770b",
"custom_task_id": "",
"trace_id": "trace_1234567890",
"client_info": "",
"init_images": null
}
}需要查询返回示例
当返回状态为 9 时,使用查询接口获取结果。
{
"request_id": "req_1234567890",
"trace_id": "trace_1234567890",
"code": 0,
"error_code": 0,
"message": "success",
"tips": null,
"data": {
"status": 9,
"result": {
"id": "task_1234567890"
},
"progress": 0,
"predict_elapsed": 10000,
"create_time": 1718172000000,
"task_id": "task_1234567890",
"custom_task_id": "",
"trace_id": "trace_1234567890",
"client_info": "",
"init_images": null
}
}请求失败返回示例
{
"request_id": "req_1234567890",
"trace_id": "trace_1234567890",
"code": 20001,
"error_code": 20001,
"message": "PROCESS_ERROR",
"tips": null,
"data": {
"status": 2,
"result": {
"id": "task_1234567890"
},
"progress": 0,
"predict_elapsed": 0,
"create_time": 1718172000000,
"task_id": "task_1234567890",
"custom_task_id": "",
"trace_id": "trace_1234567890",
"client_info": "",
"init_images": null
}
}当前 API 特有的错误代码与信息
| ErrorCode 状态代码 | 错误信息 | 说明 |
|---|---|---|
| 20001 | PROCESS_ERROR | 处理错误 |
通用的错误代码与信息
详见 API 错误码
SDK 调用示例
以下 SDK 示例均需先将 formula_id 替换为商务提供的实际配方 ID。
Python
import json
import requests
from sign_sdk import sign
def api_call_example():
key = "your_api_key"
secret = "your_api_secret"
formula_id = "your_formula_id"
url = "https://openapi.meitu.com/api/v1/sdk/sync/push"
method = "POST"
headers = {
"Content-Type": "application/json",
sign.HeaderHost: "openapi.meitu.com",
}
inner_params = {
"rsp_media_type": "url",
"parameter": {
"prompt": "girl,(cartoon:1.1)",
"seed": -1,
"width": 1024,
"height": 1024,
"resize_mode": 3,
"negative_prompt": "",
"enable_image_face_detect": False,
"steps": 50,
"denoising_strength": 0.8,
"cfg_scale": 5,
"face_generation_scale": 0.3,
"batch_size": 1,
"sample_num": 4,
"adetailer_face": 1,
},
}
payload = {
"task": f"/v1/image_style/default/{formula_id}",
"task_type": "formula",
"init_images": [
{
"url": "https://example.com/input.jpg",
"profile": {
"media_profiles": {"media_data_type": "url"},
"version": "v1",
},
}
],
"params": json.dumps(inner_params, ensure_ascii=False),
"sync_timeout": 30,
}
body = json.dumps(payload, ensure_ascii=False)
signer = sign.Signer(key, secret)
signed_request = signer.sign(url, method, headers, body)
response = requests.Session().send(signed_request)
print(f"Status: {response.status_code}")
print(f"Response: {response.text}")
if __name__ == "__main__":
api_call_example()Go
package main
import (
"fmt"
"io"
"net/http"
"github.com/mtlab/api/signer"
)
func main() {
key := "your_api_key"
secret := "your_api_secret"
formulaID := "your_formula_id"
signObj := signer.NewSigner(key, secret)
url := "https://openapi.meitu.com/api/v1/sdk/sync/push"
method := http.MethodPost
headers := make(http.Header)
headers.Set(signer.HeaderHost, "openapi.meitu.com")
headers.Set("Content-Type", "application/json")
body := fmt.Sprintf(`{
"task": "/v1/image_style/default/%s",
"task_type": "formula",
"init_images": [{
"url": "https://example.com/input.jpg",
"profile": {
"media_profiles": {"media_data_type": "url"},
"version": "v1"
}
}],
"params": "{\"rsp_media_type\":\"url\",\"parameter\":{\"prompt\":\"girl,(cartoon:1.1)\",\"seed\":-1,\"width\":1024,\"height\":1024,\"resize_mode\":3,\"negative_prompt\":\"\",\"enable_image_face_detect\":false,\"steps\":50,\"denoising_strength\":0.8,\"cfg_scale\":5,\"face_generation_scale\":0.3,\"batch_size\":1,\"sample_num\":4,\"adetailer_face\":1}}",
"sync_timeout": 30
}`, formulaID)
req, err := signObj.Sign(url, method, headers, body)
if err != nil {
fmt.Println("Failed to sign request:", err)
return
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println("Failed to send request:", err)
return
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Read response failed:", err)
return
}
fmt.Println("Response:", resp.StatusCode, string(responseBody))
}PHP
<?php
require 'signer.php';
$key = 'your_api_key';
$secret = 'your_api_secret';
$formulaId = 'your_formula_id';
$signer = new Signer($key, $secret);
$url = 'https://openapi.meitu.com/api/v1/sdk/sync/push';
$method = 'POST';
$headers = ['Content-Type' => 'application/json'];
$innerParams = json_encode([
'rsp_media_type' => 'url',
'parameter' => [
'prompt' => 'girl,(cartoon:1.1)',
'seed' => -1,
'width' => 1024,
'height' => 1024,
'resize_mode' => 3,
'negative_prompt' => '',
'enable_image_face_detect' => false,
'steps' => 50,
'denoising_strength' => 0.8,
'cfg_scale' => 5,
'face_generation_scale' => 0.3,
'batch_size' => 1,
'sample_num' => 4,
'adetailer_face' => 1,
],
]);
$body = json_encode([
'task' => "/v1/image_style/default/{$formulaId}",
'task_type' => 'formula',
'init_images' => [
[
'url' => 'https://example.com/input.jpg',
'profile' => [
'media_profiles' => ['media_data_type' => 'url'],
'version' => 'v1',
],
],
],
'params' => $innerParams,
'sync_timeout' => 30,
]);
$curl = $signer->sign($url, $method, $headers, $body);
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($status === 0) {
echo 'Error: ' . curl_error($curl);
} else {
echo "Status: {$status}\n";
echo "Response: {$response}\n";
}
curl_close($curl);
?>Java
package com.meitu.openai.common;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) throws Exception {
Signer signer = new Signer("your_api_key", "your_api_secret");
String formulaId = "your_formula_id";
String url = "https://openapi.meitu.com/api/v1/sdk/sync/push";
String method = "POST";
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put(Signer.HeaderHost, "openapi.meitu.com");
String body = "{\n" +
" \"task\": \"/v1/image_style/default/" + formulaId + "\",\n" +
" \"task_type\": \"formula\",\n" +
" \"init_images\": [{\n" +
" \"url\": \"https://example.com/input.jpg\",\n" +
" \"profile\": {\n" +
" \"media_profiles\": {\"media_data_type\": \"url\"},\n" +
" \"version\": \"v1\"\n" +
" }\n" +
" }],\n" +
" \"params\": \"{\\\"rsp_media_type\\\":\\\"url\\\",\\\"parameter\\\":{\\\"prompt\\\":\\\"girl,(cartoon:1.1)\\\",\\\"seed\\\":-1,\\\"width\\\":1024,\\\"height\\\":1024,\\\"resize_mode\\\":3,\\\"negative_prompt\\\":\\\"\\\",\\\"enable_image_face_detect\\\":false,\\\"steps\\\":50,\\\"denoising_strength\\\":0.8,\\\"cfg_scale\\\":5,\\\"face_generation_scale\\\":0.3,\\\"batch_size\\\":1,\\\"sample_num\\\":4,\\\"adetailer_face\\\":1}}\",\n" +
" \"sync_timeout\": 30\n" +
"}";
Map<String, String> signedHeaders = signer.sign(url, method, headers, body);
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod(method);
for (Map.Entry<String, String> entry : signedHeaders.entrySet()) {
connection.setRequestProperty(entry.getKey(), entry.getValue());
}
connection.setDoOutput(true);
connection.getOutputStream().write(body.getBytes(StandardCharsets.UTF_8));
int status = connection.getResponseCode();
InputStream stream = status >= 400
? connection.getErrorStream()
: connection.getInputStream();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
System.out.println("Response: " + status + " " + response);
}
}
}