用Python实现人像动漫化
本文章会教你如何使用Python实现人像动漫化,先看看效果实例: ![1](https://img-blog.csdnimg.cn/20200617155421977.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3AxOTY3OTE0OTAx,size_16,color_FFFFFF,t_70)
实现过程如下:
本案例是使用百度的API来实现的,首先需要进入百度AI开放平台注册账号,具体流程就不讲了。登陆后我们可以看到如下界面: ![1](https://img-blog.csdnimg.cn/2020061716022796.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3AxOTY3OTE0OTAx,size_16,color_FFFFFF,t_70)
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()
|