2023-09-27 11:36

Python-百度AI-智能作画

码自答

Python

(537)

(0)

收藏

  • 通过百度AI账号的AK和SK获得access_token


  • import requests;
    import json;
    
    ak = "****";
    sk = "********";
    url = f"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={ak}&client_secret={sk}";
    
    response = requests.post(url);
    
    content = response.content;
    
    print(content);


  • 通过access_token智能作画

  • access_token = "24.b13f71f492e83de4ab3807435c6fc1dc.2592000.1695867950.282335-38435971";
    
    url = f"https://aip.baidubce.com/rpc/2.0/ernievilg/v1/txt2img?access_token={access_token}";
    
    data = json.dumps({
        "text": "抽烟的超人",   #画的要求
        "style": "油画",
        "resolution": "1024*1024",
        "num": 1
    });
    
    headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        }
    
    response = requests.post(url,data=data,headers=headers);
    
    content = response.content;
    
    content = json.loads(content);
    
    #tak_id = content["data"]['taskId'];
    
    print(content);


  • 通过画的tak_id下载图画

  • tak_id = "17349725";
    access_token = "24.b13f71f492e83de4ab3807435c6fc1dc.2592000.1695867950.282335-38435971";
    
    headers = {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        }
    
    url = f"https://aip.baidubce.com/rpc/2.0/ernievilg/v1/getImg?access_token={access_token}";
    
    data = json.dumps({"taskId":tak_id});
    
    response = requests.post(url,data=data,headers=headers);
    
    print(response.content)

content中间会有生成的图的网址

0条评论

点击登录参与评论