python實現人像動漫化的示例代碼
利用百度api實現人像動漫化
百度API地址:https://ai.baidu.com/tech/imageprocess/selfie_anime
技術文檔:https://ai.baidu.com/ai-doc/IMAGEPROCESS/Mk4i6olx5
注冊百度賬號,開通實現人像動漫化,創建應用。
# encoding:utf-8 import requestsimport base64 # client_id 為官網獲取的AK, client_secret 為官網獲取的SKhost = ’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']
將上面的【官網獲取的AK】【官網獲取的SK】’ 替換成自己的API Key 和 Secret Key
’’’人像動漫化’’’request_url = 'https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime'# 二進制方式打開需要處理圖片文件f = open(’001.jpg’, ’rb’) # 打開需要處理的圖片img = base64.b64encode(f.read()) params = {'image':img}request_url = request_url + '?access_token=' + access_tokenheaders = {’content-type’: ’application/x-www-form-urlencoded’}response = requests.post(request_url, data=params, headers=headers)print(response)if response: # 保存文件 f = open(’t.jpg’, ’wb’) img = (response.json()[’image’]) f.write(base64.b64decode(img)) f.close()
到此這篇關于python實現人像動漫化的示例代碼的文章就介紹到這了,更多相關python 人像動漫化內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: