Python人物頭像動漫化


  • 人物頭像動漫化:
  • 人物頭像動漫化(戴口罩):
  • 有問題請留言(點個贊唄!~~~
    里面應用的是百度ai官網申請的數據。

     

    人物頭像動漫化:

    源程序代碼(注釋已經盡可能詳細):

    """
       __author__="dazhi"
        2021/3/20-22:18
    """
    import requests
    import base64
    
    # 1、-----------------------驗證操作(獲取access_token)鑒權
    # grant_type、client_id、client_secret是百度ai里面申請的參數
    host = 'https://aip.baidubce.com/oauth/2.0/token'
    data = {
        'grant_type': 'client_credentials',  # 固定值
        'client_id': '【改成自己的Ak哦】',  # 在開放平台注冊后所建應用的API Key
        'client_secret': '【改成自己的Sk哦】'  # 所建應用的Secret Key
    }
    response = requests.get(host, data)
    access_token = ''
    if response:
        access_token = response.json()['access_token']
        print("access_token 的值為:", access_token)
    # 2、-------------------------確定網址
    requests_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
    # 需要的參數(二進制方式打開圖片文件)
    # with open可以自動關閉
    with open('./img/4.jpg', 'rb') as file:
        # 讀取圖片(進行b64編碼)
        img = base64.b64encode(file.read())
    params = {"image": img}
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    # access_token
    requests_url = requests_url + "?access_token=" + access_token
    # 發送請求(有路徑有參數)
    response = requests.post(requests_url, data=params, headers=headers)
    # 做判斷
    if response:
        #保存操作
        with open("./img_new/001.jpg",'wb') as file:
            anime = response.json()['image']
            #將數據進行轉化(解碼操作)
            anime_image = base64.b64decode(anime)
            file.write(anime_image)

    運行結果如下:

    access_token的數值:

    在這里插入圖片描述

    原照片:

    在這里插入圖片描述

    動漫化后的照片:

    在這里插入圖片描述

    人物頭像動漫化(戴口罩):

    源程序代碼如下(注釋已經盡可能詳細):

    """
       __author__="dazhi"
        2021/3/20-22:18
    """
    import requests
    import base64
    
    # 1、-----------------------驗證操作(獲取access_token)鑒權
    # grant_type、client_id、client_secret是百度ai里面申請的參數
    host = 'https://aip.baidubce.com/oauth/2.0/token'
    data = {
        'grant_type': 'client_credentials',  # 固定值
        'client_id': '【改成自己的Ak哦】',  # 在開放平台注冊后所建應用的API Key
        'client_secret': '【改成自己的Sk哦】'  # 所建應用的Secret Key
    }
    response = requests.get(host, data)
    access_token = ''
    if response:
        access_token = response.json()['access_token']
        print("access_token 的值為:", access_token)
    # 2、-------------------------確定網址
    requests_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
    # 需要的參數(二進制方式打開圖片文件)
    # with open可以自動關閉
    with open('./img/4.jpg', 'rb') as file:
        # 讀取圖片(進行b64編碼)
        img = base64.b64encode(file.read())
    # 注意:這里就是多了type參數和mask_id參數,都是在源文檔中可以查看的參數。
    # type的值為anime或者anime_mask。前者生成二次元動漫圖,后者生成戴口罩的二次元動漫人像。
    # 1~8之間的整數,用於指定所使用的口罩的編碼。大家可以自行下去嘗試。
    params = {"image": img,"type":'anime_mask',"mask_id":"2"}
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    # access_token
    requests_url = requests_url + "?access_token=" + access_token
    # 發送請求(有路徑有參數)
    response = requests.post(requests_url, data=params, headers=headers)
    # 做判斷
    if response:
        #保存操作
        with open("./img_new/001.jpg",'wb') as file:
            anime = response.json()['image']
            #將數據進行轉化(解碼操作)
            anime_image = base64.b64decode(anime)
            file.write(anime_image)

    運行結果如下:

    在這里插入圖片描述

    有問題請留言(點個贊唄!~~~)


免責聲明!

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



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