请求URL:http(s)://api.shiliuai.com/api/image_convert/v1
请求方式:POST
返回类型:JSON
参数 | 类型 | 说明 |
---|---|---|
Content-type | string | application/json |
APIKEY | string | 您的API KEY |
参数 | 是否必填 | 类型 | 说明 |
---|---|---|---|
image_base64 | 必须填写其中之一 | string | base64编码的图片文件, image_base64和image_id必须提供一个 |
image_id | string | 图片id | |
dpi | 否 | int | [10, 1000], 生成图片分辨率,默认不改变图片分辨率 |
width | 否 | int | 生成图片宽度(像素),默认不改变图片宽度 |
height | 否 | int | 生成图片高度(像素),默认不改变图片高度 |
quality | 否 | int | [1, 100],生成图片质量,默认为92 |
minFileSize | 否 | int | 生成图片的最小文件大小,[0, inf],比如10240表示最小文件大小为10KB,只有当quality为空时起作用,默认为空 |
maxFileSize | 否 | int | 生成图片的最大文件大小,[0, inf],只有当quality为空时起作用,默认为空 |
format | 否 | String | 生成图片格式:"JPEG", "PNG", "BMP" |
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/image_convert/v1' headers = {'APIKEY': api_key, "Content-Type": "application/json"} data = { "image_base64": photo_base64, "dpi": 300 } response = requests.post(url=url, headers=headers, json=data) response = json.loads(response.content) """ 成功:{'code': 0, 'msg': 'OK', 'msg_cn': '成功', 'image_id': image id, ''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)
$url = "https://api.shiliuai.com/api/image_convert/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, "dpi"=> 300 ); $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);
import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.awt.image.BufferedImage; import java.io.*; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Base64; import java.util.Map; public class ImageConvertExample { private static final String API_KEY = "******"; // 你的API KEY private static final String FILE_PATH = "..."; // 图片路径 private static final String API_URL = "https://api.shiliuai.com/api/image_convert/v1"; public static void main(String[] args) throws IOException { byte[] fileBytes = Files.readAllBytes(Paths.get(FILE_PATH)); String photoBase64 = Base64.getEncoder().encodeToString(fileBytes); MaprequestBody = Map.of( "image_base64", photoBase64, "dpi", 300 ); try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost request = new HttpPost(API_URL); request.setHeader("APIKEY", API_KEY); request.setHeader("Content-Type", "application/json"); request.setEntity(new StringEntity(new Gson().toJson(requestBody))); try (CloseableHttpResponse response = httpClient.execute(request)) { HttpEntity entity = response.getEntity(); if (entity != null) { String responseBody = EntityUtils.toString(entity); Map jsonResponse = new Gson().fromJson(responseBody, new TypeToken
参数 | 说明 |
---|---|
code | 错误码 |
msg | 错误信息(英文) |
msg_cn | 错误信息(中文) |
result_base64 | 高清图片的base64编码,(当code==0时会有该返回值) |
image_id | 图片id,(当code==0时会有该返回值) |
错误码 | 说明 |
---|---|
0 | 成功 |
1 | 图片错误 |
2 | 处理错误 |
3 | 服务器繁忙 |
4 | 参数错误,具体错误看msg或msg_cn |
5 | 未知错误 |
101 | API-KEY不正确 |
102 | 未知用户 |
103 | 积分已用完 |
104 | 扣除积分失败 |
图像转化API采用自研算法与图像处理技术,实现高效地格式转换、分辨率调整、质量优化等操作。该API支持多种图片格式的转换,包括JPEG、PNG和BMP,能够为您提供更好的图片处理和管理服务。
1.图片格式转换,包括JPEG、PNG和BMP
2.图片分辨率调整(DPI)
3.图片宽度、高度调整(像素)
4.图片质量调整(quality)
5.图片大小调整(KB)
API类型 | 价格说明 |
---|---|
图片转换接口API | 每次调用消耗一积分 |
如有问题联系右侧“客服” |