当前位置 博文首页 > LY的博客:python百度api人像动漫化接口

    LY的博客:python百度api人像动漫化接口

    作者:[db:作者] 时间:2021-08-02 12:39

    import requests
    import base64
    
    # 获取Access Token
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=(你的key)&client_secret=(你的密码)'
    response = requests.get(host)
    access_token = response.json()['access_token']
    
    # 获取本地文件
    path = input("Enter the image path that you want to convert: ")
    f = open(path, "rb")
    img = base64.b64encode(f.read())
    
    # 调用API转换图片
    params = {"image": img}
    request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    
    # 储存图片
    f = open(path + "PT2CC.jpeg", "wb")
    f.write(base64.b64decode(response.json()['image']))
    print("The converted image has been stored in " + path + "PT2CC.jpeg")
    input("Press Enter to quit...")
    f.close() 

    ?

    cs