基于Python的人脸动漫转换
首先需要去百度申请你自己的接口。获取access_token;
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime" # 二进制方式打开图片文件
#这里的image_src需要你自己去设置,后缀是输出图片的后缀名
def changedongman(image_src,houzhui): f = open(image_src, 'rb') img = base64.b64encode(f.read()) params = {"image":img}
access_token = '【自己申请的】' 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) if response: data = response.json() # 保存识别后的图片 img_str = data['image'] img_data = base64.b64decode(img_str) t = time.time() currtime =str(round(t * 1000)) savePath = str("D:\\StudyPython\\code\\DealWithPhoto\\static\\katongphoto\\"+currtime+"."+houzhui) dataSavePath = str("../static/katongphoto/"+currtime+"."+houzhui) insert_image_sql = "insert into image(imagename,`path`,createtime) values(%s,%s,%s)" db.insertone(sql=insert_image_sql, param=('卡通', dataSavePath, get_local_time())) with open(savePath, 'wb') as f: f.write(img_data)