Python人物头像动漫化
2021/6/22 9:26:59
本文主要是介绍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的数值:
原照片:
动漫化后的照片:
人物头像动漫化(戴口罩):
源程序代码如下(注释已经尽可能详细):
""" __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人物头像动漫化的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python