使用百度ai制作动漫头像()-ai
使用百度ai制作动漫头像()
1、在百度上申请注册属于自己的idhttps://ai.baidu.com/tech/imageprocess/selfie_anime
2、在控制台中的「免费尝鲜」领取全部福利。如果不领取,程序会报错「Open api characters limit reached」
3、创建应用。「应用归属」=>个人「应用描述」=xxx「应用名称」=人脸漫画
4、获取 API Key和Secret Key其中Secret Key默认是星号,需要点击一下才能显示。分别复制后,保存到代码中。
下面贴出代码
# -*- coding: utf-8 -*-
# @Author : Jimmy
# @Email : dinglj1760@gmail.com;@163.com
# @Version : Python 3.8
# @Time : 2022/11/30 10:16
# @File : 头像漫画.py
import requests
import base64
# get_access_token()函数是为了获取access_token参数
def get_access_token():
url = 'https://aip.baidubce.com/oauth/2.0/token'
data = {
'grant_type': 'client_credentials', # 固定值
#需要修改的地方****
'client_id': 'xxx', # 在开放平台注册后所建应用的API Key,这里属于私密,不做展示
'client_secret': 'xxx' # 所建应用的Secret Key,同上
}
res = requests.post(url, data=data)
res = res.json()
access_token = res['access_token']
return access_token
# 下面的代码就是百度API文档中展示的代码,直接搬过来使用即可
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
#需要修改的地方****
f = open(r'D:\MyDoc\Pictures\微信图片_20221130101130.jpg', 'rb') # 二进制方式打开图片文件
img = base64.b64encode(f.read()) # 这里是将图像转为base64的格式,这是百度API文档中要求的
# 人物动漫化 参数
params = {"image": img,
"type": "anime_mask",#参数anime,anime_mask 口罩
"mask_id": "1" #口罩有效 id范围为1-8
}
# 调用函数获取 access_token
access_token = get_access_token()
request_url = request_url + "?access_token=" + get_access_token()
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
res = response.json()
# 将这个图像信息写入,得到最终的效果图。
if response:
#需要修改的地方****
f = open("D:\MyDoc\Pictures\动漫图.jpg", 'wb')
after_img = res['image']
after_img = base64.b64decode(after_img)
f.write(after_img)
f.close()
————————
1、在百度上申请注册属于自己的idhttps://ai.baidu.com/tech/imageprocess/selfie_anime
2、在控制台中的「免费尝鲜」领取全部福利。如果不领取,程序会报错「Open api characters limit reached」
3、创建应用。「应用归属」=>个人「应用描述」=xxx「应用名称」=人脸漫画
4、获取 API Key和Secret Key其中Secret Key默认是星号,需要点击一下才能显示。分别复制后,保存到代码中。
下面贴出代码
# -*- coding: utf-8 -*-
# @Author : Jimmy
# @Email : dinglj1760@gmail.com;@163.com
# @Version : Python 3.8
# @Time : 2022/11/30 10:16
# @File : 头像漫画.py
import requests
import base64
# get_access_token()函数是为了获取access_token参数
def get_access_token():
url = 'https://aip.baidubce.com/oauth/2.0/token'
data = {
'grant_type': 'client_credentials', # 固定值
#需要修改的地方****
'client_id': 'xxx', # 在开放平台注册后所建应用的API Key,这里属于私密,不做展示
'client_secret': 'xxx' # 所建应用的Secret Key,同上
}
res = requests.post(url, data=data)
res = res.json()
access_token = res['access_token']
return access_token
# 下面的代码就是百度API文档中展示的代码,直接搬过来使用即可
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
#需要修改的地方****
f = open(r'D:\MyDoc\Pictures\微信图片_20221130101130.jpg', 'rb') # 二进制方式打开图片文件
img = base64.b64encode(f.read()) # 这里是将图像转为base64的格式,这是百度API文档中要求的
# 人物动漫化 参数
params = {"image": img,
"type": "anime_mask",#参数anime,anime_mask 口罩
"mask_id": "1" #口罩有效 id范围为1-8
}
# 调用函数获取 access_token
access_token = get_access_token()
request_url = request_url + "?access_token=" + get_access_token()
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
res = response.json()
# 将这个图像信息写入,得到最终的效果图。
if response:
#需要修改的地方****
f = open("D:\MyDoc\Pictures\动漫图.jpg", 'wb')
after_img = res['image']
after_img = base64.b64decode(after_img)
f.write(after_img)
f.close()