1.接口说明
图片变高清 API:对输入图片进行超分辨率增强,提升细节与清晰度,并可选择放大倍数(默认不放大)。
1.1主要功能
- 细节增强:
- 提升纹理细节,让图片更清晰。
- 可选放大:
- 支持 1~4 倍放大,自动保证长边不超过 4096px。
- 多输入方式:
- 支持 Base64 或图片 URL 输入(二选一)。
1.2接入场景
适用于多种“低清变高清”的业务场景,例如:电商商品图清晰化、老照片/扫描件增强、内容平台素材优化等场景。
2.请求信息
2.1请求地址(URL)
POST https://api.shiliuai.com/api/super_resolution/v1
2.2请求方式
POST
2.3请求头(header)
| 参数 | 类型 | 说明 |
|---|---|---|
| Content-Type | string | application/json |
| APIKEY | string | 您的 API KEY |
2.4请求体(body)
| 参数 | 是否必填 | 类型 | 说明 |
|---|---|---|---|
| image_base64 | 必须填写其中之一 | string | base64编码的图片文件,图片可以是单通道,三通道或四通道, 图片文件要小于20M,图片的长边不能超过4096像素 |
| image_url | string | 图片的url,图片可以是单通道,三通道或四通道, 图片文件要小于20M,图片的长边不能超过4096像素 | |
| scale_factor | 否 | int | 放大倍数,可取值1,2,3,4,默认为1, 放大倍数和图片长边乘积不能超过4096像素,如果超过,会自动降低放大倍数 |
3.返回信息
3.1返回类型
JSON
3.2返回信息
| 参数 | 说明 |
|---|---|
| code | 错误码 |
| msg | 错误信息(英文) |
| msg_cn | 错误信息(中文) |
| result_base64 | 高清图片的base64编码,(当code==0时会有该返回值), 如果输入图片是四通道,那么返回四通道png格式图片,否则返回jpg格式图片, 返回图片通道数和输入图片相同 |
3.3返回示例
{
"code": 0,
"msg": "OK",
"msg_cn": "成功",
"result_base64": "iVBORw0KGgoAAAANSUhEUgAA..."
}
// 失败示例
{
"code": 4,
"msg": "Invalid parameter: image_base64 or image_url is required",
"msg_cn": "参数错误:image_base64 或 image_url 必须填写其中之一"
}
3.4错误码
| 错误码 | 说明 |
|---|---|
| 0 | 成功 |
| 1 | 图片错误 |
| 2 | 处理错误 |
| 3 | 服务器繁忙 |
| 4 | 参数错误(具体错误看 msg 或 msg_cn) |
| 5 | 未知错误 |
| 101 | API-KEY 不正确 |
| 102 | 未知用户 |
| 103 | 积分已用完 |
| 104 | 扣除积分失败 |
4.示例代码
4.1 Python
# -*- coding: utf-8 -*-
import requests
import base64
import cv2
import json
import numpy as np
api_key = '******' # 你的API KEY
file_path = '...' # 图片路径
with open(file_path, 'rb') as fp:
photo_base64 = base64.b64encode(fp.read()).decode('utf8')
url = 'https://api.shiliuai.com/api/super_resolution/v1'
headers = {'APIKEY': api_key, "Content-Type": "application/json"}
data = {
"image_base64": photo_base64,
"scale_factor": 2 # 放大2倍
}
response = requests.post(url=url, headers=headers, json=data)
response = json.loads(response.content)
"""
成功:{'code': 0, 'msg': 'OK', 'msg_cn': '成功', 'result_base64': result_base64}
or
失败:{'code': error_code, 'msg': error_msg, 'msg_cn': 错误信息}
"""
result_base64 = response['result_base64']
file_bytes = base64.b64decode(result_base64)
f = open('result.jpg', 'wb')
f.write(file_bytes)
f.close()
image = np.asarray(bytearray(file_bytes), dtype=np.uint8)
image = cv2.imdecode(image, cv2.IMREAD_UNCHANGED)
cv2.imshow('result', image)
cv2.waitKey(0)
4.2 PHP
<?php $url = "https://api.shiliuai.com/api/super_resolution/v1"; $method = "POST"; $apikey = "******"; $header = array(); array_push($header, "APIKEY:" . $apikey); array_push($header, "Content-Type:application/json"); $file_path = "..."; $handle = fopen($file_path, "r"); $photo = fread($handle, filesize($file_path)); fclose($handle); $photo_base64 = base64_encode($photo); $data = array( "image_base64"=> $photo_base64, "scale_factor"=> 2 ); $post_data = json_encode($data); $curl = curl_init(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPHEADER, $header); curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); $response = curl_exec($curl); var_dump($response);
4.3 C#
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
string apiKey = "******"; // 你的API KEY
string filePath = "..."; // 图片路径
int scaleFactor = 2; // 放大倍数,例如2倍
string url = "https://api.shiliuai.com/api/super_resolution/v1";
// 将图片编码为Base64
string photoBase64;
using (var imageStream = File.OpenRead(filePath))
{
byte[] imageBytes = new byte[imageStream.Length];
await imageStream.ReadAsync(imageBytes, 0, (int)imageStream.Length);
photoBase64 = Convert.ToBase64String(imageBytes);
}
// 构造请求数据
var requestData = new
{
image_base64 = photoBase64,
scale_factor = scaleFactor
};
string jsonData = JsonSerializer.Serialize(requestData);
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("APIKEY", apiKey);
client.DefaultRequestHeaders.Add("Content-Type", "application/json");
try
{
// 发送POST请求
var response = await client.PostAsync(url, new StringContent(jsonData, Encoding.UTF8, "application/json"));
string responseString = await response.Content.ReadAsStringAsync();
// 解析响应
var responseObject = JsonSerializer.Deserialize<JsonElement>(responseString);
int code = responseObject.GetProperty("code").GetInt32();
if (code == 0)
{
string resultBase64 = responseObject.GetProperty("result_base64").GetString();
// 将Base64转换为图片并保存
byte[] fileBytes = Convert.FromBase64String(resultBase64);
File.WriteAllBytes("result.jpg", fileBytes);
Console.WriteLine("Image processing succeeded, saved as result.jpg");
}
else
{
string errorMsg = responseObject.GetProperty("msg_cn").GetString();
Console.WriteLine($"Error: {errorMsg}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
}
}
}