Python人物头像动漫化

  • 人物头像动漫化:
  • 源程序代码(注释已经尽可能详细):
  • 运行结果如下:
  • access_token的数值:
  • 原照片:
  • 动漫化后的照片:
  • 人物头像动漫化(戴口罩):
  • 源程序代码如下(注释已经尽可能详细):
  • 运行结果如下:
  • 有问题请留言(点个赞呗!~~~)



里面应用的是百度ai官网申请的数据。

人物头像动漫化:

源程序代码(注释已经尽可能详细):

"""
   __author__="dazhi"
    2021/3/20-22:18
"""
import requests
import base64

# 1、-----------------------验证操作(获取access_token)鉴权
# grant_type、client_id、client_secret是百度ai里面申请的参数
host = 'https://aip.baidubce.com/oauth/2.0/token'
data = {
    'grant_type': 'client_credentials',  # 固定值
    'client_id': '【改成自己的Ak哦】',  # 在开放平台注册后所建应用的API Key
    'client_secret': '【改成自己的Sk哦】'  # 所建应用的Secret Key
}
response = requests.get(host, data)
access_token = ''
if response:
    access_token = response.json()['access_token']
    print("access_token 的值为:", access_token)
# 2、-------------------------确定网址
requests_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
# 需要的参数(二进制方式打开图片文件)
# with open可以自动关闭
with open('./img/4.jpg', 'rb') as file:
    # 读取图片(进行b64编码)
    img = base64.b64encode(file.read())
params = {"image": img}
headers = {'content-type': 'application/x-www-form-urlencoded'}
# access_token
requests_url = requests_url + "?access_token=" + access_token
# 发送请求(有路径有参数)
response = requests.post(requests_url, data=params, headers=headers)
# 做判断
if response:
    #保存操作
    with open("./img_new/001.jpg",'wb') as file:
        anime = response.json()['image']
        #将数据进行转化(解码操作)
        anime_image = base64.b64decode(anime)
        file.write(anime_image)

运行结果如下:

access_token的数值:

python生成卡通头像 python将图片动漫化_python生成卡通头像

原照片:

python生成卡通头像 python将图片动漫化_python_02

动漫化后的照片:

python生成卡通头像 python将图片动漫化_程序代码_03

人物头像动漫化(戴口罩):

源程序代码如下(注释已经尽可能详细):

(主要是增加了两个参数。详情见代码)

"""
   __author__="dazhi"
    2021/3/20-22:18
"""
import requests
import base64

# 1、-----------------------验证操作(获取access_token)鉴权
# grant_type、client_id、client_secret是百度ai里面申请的参数
host = 'https://aip.baidubce.com/oauth/2.0/token'
data = {
    'grant_type': 'client_credentials',  # 固定值
    'client_id': '【改成自己的Ak哦】',  # 在开放平台注册后所建应用的API Key
    'client_secret': '【改成自己的Sk哦】'  # 所建应用的Secret Key
}
response = requests.get(host, data)
access_token = ''
if response:
    access_token = response.json()['access_token']
    print("access_token 的值为:", access_token)
# 2、-------------------------确定网址
requests_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
# 需要的参数(二进制方式打开图片文件)
# with open可以自动关闭
with open('./img/4.jpg', 'rb') as file:
    # 读取图片(进行b64编码)
    img = base64.b64encode(file.read())
# 注意:这里就是多了type参数和mask_id参数,都是在源文档中可以查看的参数。
# type的值为anime或者anime_mask。前者生成二次元动漫图,后者生成戴口罩的二次元动漫人像。
# 1~8之间的整数,用于指定所使用的口罩的编码。大家可以自行下去尝试。
params = {"image": img,"type":'anime_mask',"mask_id":"2"}
headers = {'content-type': 'application/x-www-form-urlencoded'}
# access_token
requests_url = requests_url + "?access_token=" + access_token
# 发送请求(有路径有参数)
response = requests.post(requests_url, data=params, headers=headers)
# 做判断
if response:
    #保存操作
    with open("./img_new/001.jpg",'wb') as file:
        anime = response.json()['image']
        #将数据进行转化(解码操作)
        anime_image = base64.b64decode(anime)
        file.write(anime_image)

运行结果如下:

python生成卡通头像 python将图片动漫化_python生成卡通头像_04

有问题请留言(点个赞呗!~~~)