用Python实现人像动漫化

您所在的位置:网站首页 python人物画像为什么图像不像人呢 用Python实现人像动漫化

用Python实现人像动漫化

2024-07-10 18:21| 来源: 网络整理| 查看: 265

用Python实现人像动漫化

本文章会教你如何使用Python实现人像动漫化,先看看效果实例: 1 1

实现过程如下:

本案例是使用百度的API来实现的,首先需要进入百度AI开放平台注册账号,具体流程就不讲了。登陆后我们可以看到如下界面: 1 1

import base64 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: access_token = response.json()["access_token"] ''' 人像动漫化 ''' request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime" # 二进制方式打开需要处理图片文件 f = open('原图.jpg', 'rb') # 打开需要处理的图片 img = base64.b64encode(f.read()) params = {"image":img} 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) print(response) if response: # 保存文件 f = open('动漫图.jpg', 'wb') img = (response.json()['image']) f.write(base64.b64decode(img)) f.close()


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3