人像动漫化
- 一、生成二次元动漫图
- 二、生成戴口罩的二次元动漫人像
一、生成二次元动漫图
import requests
import base64
def access_token():AK = ' ' SK = ' ' host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+AK+'&client_secret='+SKresponse = requests.get(host)if response:print(response.json(),'\n\n')access_token = response.json()return access_token['access_token']
def portraitAnimation(token,imgUrl):f = open(imgUrl, 'rb') img = base64.b64encode(f.read()) params = {"image": img}access_token = tokenrequest_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"request_url = request_url + "?access_token=" + access_tokenheaders = {'content-type': 'application/x-www-form-urlencoded'}response = requests.post(request_url, data=params, headers=headers)if response:ima = response.json()data = base64.b64decode(ima['image']) file = open("p2.png", "wb")file.write(data)print('人像动漫化 生成完毕!')
if __name__ == '__main__':accessToken = access_token()imgUrl = "C:\\Users\\97942\\PycharmProjects\\Comic\\周深.png" portraitAnimation(accessToken,imgUrl)
二、生成戴口罩的二次元动漫人像
import requests
import base64
def access_token():AK = ' ' SK = ' ' host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+AK+'&client_secret='+SKresponse = requests.get(host)if response:print(response.json(),'\n\n')access_token = response.json()return access_token['access_token']
def portraitAnimation(token,imgUrl):f = open(imgUrl, 'rb') img = base64.b64encode(f.read()) params = {"image": img,"type":'anime_mask',"mask_id":"2"}access_token = tokenrequest_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"request_url = request_url + "?access_token=" + access_tokenheaders = {'content-type': 'application/x-www-form-urlencoded'}response = requests.post(request_url, data=params, headers=headers)if response:ima = response.json()data = base64.b64decode(ima['image']) file = open("p2.png", "wb")file.write(data)print('人像动漫化 生成完毕!')
if __name__ == '__main__':accessToken = access_token()imgUrl = "C:\\Users\\97942\\PycharmProjects\\Comic\\p1.png" portraitAnimation(accessToken,imgUrl)