python實(shí)現(xiàn)人像動漫化的示例代碼
利用百度api實(shí)現(xiàn)人像動漫化
百度API地址:https://ai.baidu.com/tech/imageprocess/selfie_anime
技術(shù)文檔:https://ai.baidu.com/ai-doc/IMAGEPROCESS/Mk4i6olx5
注冊百度賬號,開通實(shí)現(xiàn)人像動漫化,創(chuàng)建應(yīng)用。
# encoding:utf-8 import requestsimport base64 # client_id 為官網(wǎng)獲取的AK, client_secret 為官網(wǎng)獲取的SKhost = ’https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官網(wǎng)獲取的AK】&client_secret=【官網(wǎng)獲取的SK】’response = requests.get(host)if response: access_token= response.json()['access_token']
將上面的【官網(wǎng)獲取的AK】【官網(wǎng)獲取的SK】’ 替換成自己的API Key 和 Secret Key
’’’人像動漫化’’’request_url = 'https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime'# 二進(jìn)制方式打開需要處理圖片文件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()
到此這篇關(guān)于python實(shí)現(xiàn)人像動漫化的示例代碼的文章就介紹到這了,更多相關(guān)python 人像動漫化內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 測試模式 - XSL教程 - 52. python b站視頻下載的五種版本3. vue實(shí)現(xiàn)簡易圖片左右旋轉(zhuǎn),上一張,下一張組件案例4. 每日六道java新手入門面試題,通往自由的道路第二天5. Python結(jié)合百度語音識別實(shí)現(xiàn)實(shí)時(shí)翻譯軟件的實(shí)現(xiàn)6. 解決Java中的java.io.IOException: Broken pipe問題7. python如何寫個(gè)俄羅斯方塊8. 《CSS3實(shí)戰(zhàn)》筆記--漸變設(shè)計(jì)(一)9. 教你JS更簡單的獲取表單中數(shù)據(jù)(formdata)10. JAVA抽象類及接口使用方法解析
