AI圖像處理-人像動漫化


圖像處理

人像動漫化

百度接口提供的

 

 具體實現

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!')

結果如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM