圖像處理
人像動漫化
百度接口提供的
具體實作
import requests, base64
# 百度AI開放平台鑒權函數
def get_access_token():
url = 'https://aip.baidubce.com/oauth/2.0/token'
data = {
'grant_type': 'client_credentials', # 固定值
'client_id': '6M7FeQm****c3p0ex1', # 在開放平台注冊後所建應用的API Key
'client_secret': 'XyyUSrFB*****KyBRI0KFuc' # 所建應用的Secret Key
}
res = requests.post(url, data=data)
res = res.json()
access_token = res['access_token']
return access_token
def image_process(img_before, img_after, how_to_deal):
# 函數的三個參數,一個是轉化前的檔案名,一個是轉化後的檔案名,均在同一目錄下,第三個是圖像處理能力選擇
request_url = 'https://aip.baidubce.com/rest/2.0/image-process/v1/' + how_to_deal
if how_to_deal == 'style_trans': # 判斷如果是 圖像風格化,需要額外添加一個風格配置
others = 'cartoon' # 風格化參數,具體可設定範圍參見下面注釋
'''
cartoon:卡通畫風格
pencil:鉛筆風格
color_pencil:彩色鉛筆畫風格
warm:彩色糖塊油畫風格
wave:神奈川沖浪裡油畫風格
lavender:薰衣草油畫風格
mononoke:奇異油畫風格
scream:呐喊油畫風格
gothic:哥特油畫風格
'''
else:
others = ''
file = open(img_before, 'rb') # 二進制讀取圖檔
origin_img = base64.b64encode(file.read()) # 将圖檔進行base64編碼
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
data = {
'access_token': get_access_token(),
'image': origin_img
}
res = requests.post(request_url, data=data, headers=headers)
res = res.json()
if res:
f = open(img_after, 'wb')
after_img = res['image']
after_img = base64.b64decode(after_img)
f.write(after_img)
f.close()
if __name__ == '__main__':
img_before = 'img/me.jpg' # 目前目錄下的圖檔
img_after = img_before.split('.') # 将原檔案名分成清單
img_after = img_after[0] + '_1.' + img_after[1] # 新生成的檔案名為原檔案名上加 _1
image_process(img_before, img_after, 'selfie_anime')
# 第三個參數: selfie_anime 為人像動漫化,colourize 圖像上色,style_trans 為圖像風格化
print('done!')
生成結果:
還可以給他戴上口罩
隻需在data裡加上參數:"type":'anime_mask',"mask_id":"2"
import requests, base64
# 百度AI開放平台鑒權函數
def get_access_token():
url = 'https://aip.baidubce.com/oauth/2.0/token'
data = {
'grant_type': 'client_credentials', # 固定值
'client_id': '6M7FeQm*****EeDzc3p0ex1', # 在開放平台注冊後所建應用的API Key
'client_secret': 'XyyUS****BRI0KFuc' # 所建應用的Secret Key
}
res = requests.post(url, data=data)
res = res.json()
access_token = res['access_token']
return access_token
def image_process(img_before, img_after, how_to_deal):
# 函數的三個參數,一個是轉化前的檔案名,一個是轉化後的檔案名,均在同一目錄下,第三個是圖像處理能力選擇
request_url = 'https://aip.baidubce.com/rest/2.0/image-process/v1/' + how_to_deal
if how_to_deal == 'style_trans': # 判斷如果是 圖像風格化,需要額外添加一個風格配置
others = 'cartoon' # 風格化參數,具體可設定範圍參見下面注釋
'''
cartoon:卡通畫風格
pencil:鉛筆風格
color_pencil:彩色鉛筆畫風格
warm:彩色糖塊油畫風格
wave:神奈川沖浪裡油畫風格
lavender:薰衣草油畫風格
mononoke:奇異油畫風格
scream:呐喊油畫風格
gothic:哥特油畫風格
'''
else:
others = ''
file = open(img_before, 'rb') # 二進制讀取圖檔
origin_img = base64.b64encode(file.read()) # 将圖檔進行base64編碼
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
data = {
'access_token': get_access_token(),
'image': origin_img,
"type":'anime_mask',
"mask_id":"2"
}
res = requests.post(request_url, data=data, headers=headers)
res = res.json()
if res:
f = open(img_after, 'wb')
after_img = res['image']
after_img = base64.b64decode(after_img)
f.write(after_img)
f.close()
if __name__ == '__main__':
img_before = 'img/me.jpg' # 目前目錄下的圖檔
img_after = img_before.split('.') # 将原檔案名分成清單
img_after = img_after[0] + '_2.' + img_after[1] # 新生成的檔案名為原檔案名上加 _1
image_process(img_before, img_after, 'selfie_anime')
# 第三個參數: selfie_anime 為人像動漫化,colourize 圖像上色,style_trans 為圖像風格化
print('done!')
結果如下: