
人物头像动漫化:源程序代码(注释已经尽可能详细):
""" __author__="dazhi" 2021/3/20-22:18"""import requestsimport 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_tokenrequests_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 requestsimport 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_tokenrequests_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人物头像动漫化所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)