字节 AIGCStylizeImage API 调用文档

整合并简化了字节官网的 API 接入文档,从注册账号到跑通,一定可以加快你的上手速度,实际工程调用实例可以直接跳转到最后一个目录单元

前期准备

火山引擎官网, 注册账号并完成实名认证,在下拉栏中选择 API访问密钥,按指引创建密钥,获取必要的 aksk

返回刚刚的页面,开通 Doubao—AIGC图像风格化模型,可以看到以下界面:

调用方法

官方文档

必要请求参数

注意:url 和 base64 二选一即可

1
2
3
4
5
6
7
8
9
10
11
12
{
"req_key": "img2img_disney_3d_style", #指定的风格
"sub_req_key":"" # !!! 根据情况选择 看后文对 sub_req_key 的解释
"image_urls": [
"https://xxx"
],
"binary_data_base64": [
#处理过的图片 base64编码
],
"return_url": true,

}

req_key

指定的风格,即 req_key 可选参数如下(代码块可展开):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
网红日漫风:img2img_ghibli_style

3D风: img2img_disney_3d_style

写实风:img2img_real_mix_style

天使风:img2img_pastel_boys_style

动漫风:img2img_cartoon_style

日漫风:img2img_makoto_style

公主风:img2img_rev_animated_style

梦幻风:img2img_blueline_style

水墨风:img2img_water_ink_style

新莫奈花园: i2i_ai_create_monet

水彩风:img2img_water_paint_style

莫奈花园:img2img_comic_style

精致美漫:img2img_comic_style

赛博机械:img2img_comic_style

精致韩漫:img2img_exquisite_style

国风-水墨:img2img_pretty_style

浪漫光影:img2img_pretty_style

陶瓷娃娃:img2img_ceramics_style

中国红:img2img_chinese_style

丑萌粘土:img2img_clay_style

可爱玩偶:img2img_clay_style

3D-游戏_Z时代:img2img_3d_style

动画电影:img2img_3d_style

玩偶:img2img_3d_style

sub_req_key

目的是区分大风格下的分支风格,只有部分风格需要这个参数,其余风格不需要传参

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
莫奈花园:img2img_comic_style_monet

精致美漫:img2img_comic_style_marvel

赛博机械:img2img_comic_style_future

国风-水墨:img2img_pretty_style_ink

浪漫光影:img2img_pretty_style_light

丑萌粘土:img2img_clay_style_3d

可爱玩偶:img2img_clay_style_bubble

3D-游戏_Z时代:img2img_3d_style_era

动画电影:img2img_3d_style_movie

玩偶:img2img_3d_style_doll

返回参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
"code": 10000,
"data": {
"algorithm_base_resp": {
"status_code": 0,
"status_message": "Success"
},
"binary_data_base64": [
"/9xxx", #重点关注此返回,转换后的图片Base64
],
"image_urls": [
"https://xxx" #重点关注此返回,转换后的图片url
],
#.... 其他不重要的返回数据
},
"message": "Success",
"request_id": "202405201722544E4A22904AB5C20B42D5",
"status": 10000,
"time_elapsed": "5.849820164s"
}

请求示例

可以借助 签名工具获取签名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
POST /?Action=CVProcess&Version=2022-08-31 HTTP/1.1
Host: visual.volcengineapi.com
Content-Type: application/json; charset=UTF-8
X-Date: 20241120T122058Z
X-Content-Sha256: 287e874e******d653b44d21e
Authorization: HMAC-SHA256 Credential=Adfks******wekfwe/20241120/cn-beijing/cv/request, SignedHeaders=host;x-content-sha256;x-date, Signature=47a7d934ff7b37c03938******cd7b8278a40a1057690c401e92246a0e41085f
{
"req_key": "img2img_disney_3d_style",
"image_urls": [
"https://xxx"
],
"return_url": true,
"logo_info": {
"add_logo": true,
"position": 0,
"language": 0,
"logo_text_content": "这里是明水印内容"
}
}

也可使用字节提供的 SDK (强烈推荐这个方式)

SDK 的使用 (强烈推荐)

python为例,其他语言可以去官网自行查阅

手动下载工具包

1
2
pip install volcengine-python-sdk==2.0.1
pip install volcengine

实例调用代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# coding:utf-8
from __future__ import print_function

from volcengine.visual.VisualService import VisualService

if __name__ == '__main__':
visual_service = VisualService()

# call below method if you don't set ak and sk in $HOME/.volc/config
visual_service.set_ak('your ak')
visual_service.set_sk('your sk')

# 请求Body(查看接口文档请求参数-请求示例,将请求参数内容复制到此)
form = {
"req_key": "xxx",
# ...
}

resp = visual_service.cv_process(form)
print(resp)

调用实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# coding:utf-8
from __future__ import print_function
import base64
import os
import requests
from volcengine.visual.VisualService import VisualService

if __name__ == '__main__':
visual_service = VisualService()
visual_service.set_ak('你的 ak') #自行配置
visual_service.set_sk('你的 sk')

input_folder = "你的文件夹" #自行配置
if not os.path.exists(input_folder):
print(f"输入文件夹 {input_folder} 不存在")
exit(1)

# 创建 doubao_output 文件夹
output_folder = 'doubao_output'
if not os.path.exists(output_folder):
os.makedirs(output_folder)

for filename in os.listdir(input_folder):
if filename.endswith(('.png', '.jpg', '.jpeg')):
image_path = os.path.join(input_folder, filename)
try:
with open(image_path, "rb") as image_file:
image_bytes = image_file.read()
encoded_image = base64.b64encode(image_bytes).decode('utf-8')

form = {
"req_key": "img2img_water_ink_style",
"binary_data_base64": [encoded_image],
"return_url": True,
}

resp = visual_service.cv_process(form)
print(f"处理 {filename} 的响应: {resp}")

if 'data' in resp and 'image_urls' in resp['data']:
for index, url in enumerate(resp['data']['image_urls']):
clean_url = url.strip(' `')
try:
response = requests.get(clean_url)
if response.status_code == 200:
new_filename = f"db_{filename}"
file_path = os.path.join(output_folder, new_filename)
with open(file_path, 'wb') as f:
f.write(response.content)
print(f'图片 {new_filename} 已保存到 {file_path}')
else:
print(f'下载图片 {clean_url} 失败,状态码: {response.status_code}')
except requests.RequestException as e:
print(f'下载图片 {clean_url} 时发生错误: {e}')
except Exception as e:
print(f"处理 {filename} 时发生错误: {e}")

效果展示: