python集成百度云-实现图片的动漫化
  LeEJFEmgyGb2 2023年11月02日 43 0

1:百度云申请

https://console.bce.baidu.com/ai/#/ai/imageprocess/app/list

python集成百度云-实现图片的动漫化_python

2:python代码实现

import requests
import pprint
import base64


def get_access_token(id, secret):
    get_access_token_url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + id + '&client_secret=' + secret
    response = requests.get(get_access_token_url)
    content = response.json()
    access_token = content['access_token']
    return access_token


def Animation(img_file, access_token):
    request_url = 'https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime'
    f = open(img_file, 'rb')
    image = base64.b64encode(f.read())
    params = {"image": image}
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    image_content = response.json()
    image = image_content['image']
    with open('result.jpg', 'wb') as f:
        f.write(base64.b64decode(image))


def main():
    img_file = 'ysp.png'  # 图片地址
    id = 'hwgOvS2PqYdHh07EEqU3wGYSPmy'
    secret = 'aD3L3yoLSTE9UFqC6C3ShalAM3r4KY95YSP'
    access_token = get_access_token(id, secret)
    Animation(img_file, access_token)


if __name__ == '__main__':
    main()

3:效果

原始图

python集成百度云-实现图片的动漫化_json_02

效果图

python集成百度云-实现图片的动漫化_python_03


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  2Fnpj8K6xSCR   2024年05月17日   104   0   0 Python
  xKQN3Agd2ZMK   2024年05月17日   73   0   0 Python
  fwjWaDlWXE4h   2024年05月17日   38   0   0 Python
  YpHJ7ITmccOD   2024年05月17日   39   0   0 Python
LeEJFEmgyGb2