首页 > 卡通形象 > 生成卡通头像的软件-元界首个AI漫画头像可在线制作
2023
07-04

生成卡通头像的软件-元界首个AI漫画头像可在线制作

请求网址:

网址参数:

标头如下:

将请求参数放在body中,参数如下:

请求参数

3.8 在线调试API

即可进入在线调试界面,如果觉得麻烦可以跳过这一步生成卡通头像的软件,直接通过代码进行调试

3.9 Python调用

官方代码如下:

# encoding:utf-8
import requests
import base64
'''
人像动漫化
'''
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
# 二进制方式打开图片文件
f = open('[本地文件]', 'rb')
img = base64.b64encode(f.read())
params = {"image":img}
access_token = '[调用鉴权接口获取的token]'
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
    print (response.json())

我们稍微修改一下

# encoding:utf-8
import requests
import base64
def image_process(img_name,client_id,client_secret):
    request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
    # 二进制方式打开图片文件
    f = open(img_name, 'rb')
    img = base64.b64encode(f.read())
    params = {"image":img}
    access_token = get_token(client_id,client_secret)
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        print (response.json())
    return response.json()['image']

3.10 获取代币

官方代币获取方式:

# encoding:utf-8
import requests 
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】'
response = requests.get(host)
if response:
    print(response.json())

我们稍微修改一下

def get_token(client_id,client_secret):
    # client_id 为官网获取的AK, client_secret 为官网获取的SK
    host = f'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}'
    response = requests.get(host)
    if response:
        print(response.json())
        return response.json()['access_token']
    else:
        print('no response!')

然后卡通形象,运行代码即可得到处理后图像的base64编码~

# client_id 为官网获取的AK, client_secret 为官网获取的SK
client_id='xxx'
client_secret='xxx'
img_name='img_name.jpg'
img_base64=image_process(img_name,client_id,client_secret)
print(img_base64)

可以发现官方已经给出了非常详细的代码模板生成卡通头像的软件卡通人物,而我们真正需要改动的部分就是

client_id='xxx'
client_secret='xxx'

只有两行~

3.11 查看图片

获取代码后,可以在浏览器中输入data:image/jpeg;[你的base64代码,注意去掉方括号]即可查看图片

或者使用以下代码将base64编码转换为图像

def base2picture(img_base64,img_save):
    imgdata = base64.b64decode(img_base64)
    with open(img_save,'wb') as f:
        f.write(imgdata)

img_save='img_save.jpg'
base2picture(img_base64,img_save)

4 完整代码

# encoding:utf-8
import requests
import base64
def image_process(img_name,client_id,client_secret):
    request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
    # 二进制方式打开图片文件
    f = open(img_name, 'rb')
    img = base64.b64encode(f.read())
    params = {"image":img}
    access_token = get_token(client_id,client_secret)
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        print (response.json())
    return response.json()['image']
def get_token(client_id,client_secret):
    # client_id 为官网获取的AK, client_secret 为官网获取的SK
    host = f'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}'
    response = requests.get(host)
    if response:
        print(response.json())
        return response.json()['access_token']
    else:
        print('no response!')
def base2picture(img_base64,img_save):
    imgdata = base64.b64decode(img_base64)
    with open(img_save,'wb') as f:
        f.write(imgdata)
if __name__=='__main__':
    client_id='xxx'
    client_secret='xxx'
    img_name='1.jpg'
    img_base64=image_process(img_name,client_id,client_secret)
    print(img_base64)
    # 或者在浏览器输入 data:image/jpeg;[base64]
    img_save='2.jpg'
    base2picture(img_base64,img_save)

大家都学会了吗?

去尝试一下吧~

最后编辑:
作者:nuanquewen
吉祥物设计/卡通ip设计/卡通人物设计/卡通形象设计/表情包设计