1. 程式人生 > 程式設計 >python實現人像動漫化的示例程式碼

python實現人像動漫化的示例程式碼

利用百度api實現人像動漫化

python實現人像動漫化的示例程式碼

百度API地址:https://ai.baidu.com/tech/imageprocess/selfie_anime

技術文件:https://ai.baidu.com/ai-doc/IMAGEPROCESS/Mk4i6olx5

註冊百度賬號,開通實現人像動漫化,建立應用。

python實現人像動漫化的示例程式碼

# encoding:utf-8
 
import requests
import base64
 
# 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"]

將上面的【官網獲取的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_token
headers = {'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 人像動漫化內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!