You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
python-project/美颜/view.py

88 lines
3.4 KiB

desc = '''
***********************************************
authOr: newrain *
blog: https://blog.csdn.net/NewRain_wang *
https://newrain001.gitee.io *
github: https://github.com/newrain001 *
gitee : https://gitee.com/newrain001 *
email : newrain_wang@163.com *
***********************************************
1、此脚本采用旷视ai人脸美颜技术接口
https://www.faceplusplus.com.cn/
2、文件不得大于2M
3、像素不大大于4096x4096
4、首次执行程序后会生成配置文件,请注册并填写api_key和api_secret
5、配置文件解析
'whitening': 100, # 美白
'smoothing': 100, # 磨皮
'thinface': 100, # 瘦脸
'shrink_face': 100, # 小脸
'enlarge_eye': 100, # 大眼
'remove_eyebrow': 100, # 去眉毛
'filter_type': 'beautify', # 滤镜 https://console.faceplusplus.com.cn/documents/134252584
6、原图请放置在images文件夹中,新图会在源文件前加上new
'''
from requests import post
from time import sleep
from json import dump,load
import os
import base64
# from tqdm import tqdm
class Kuangshi():
def __init__(self):
if not os.path.exists('./readme.txt'):
with open('./readme.txt', 'w', encoding='utf-8') as f:
f.write(desc)
os.system('notepad ./readme.txt')
if not os.path.exists('./config.json'):
config = {
'api_key': '',
'api_secret': '',
'whitening': 100, # 美白
'smoothing': 100, # 磨皮
'thinface': 100, # 瘦脸
'shrink_face': 100, # 小脸
'enlarge_eye': 100, # 大眼
'remove_eyebrow': 100, # 去眉毛
'filter_type': 'beautify', # 滤镜 https://console.faceplusplus.com.cn/documents/134252584
}
with open('./config.json', 'w') as f:
dump(config, f, indent='\t', ensure_ascii=False)
print('请先修改配置文件运行')
sleep(5)
else:
with open('./config.json', 'r') as f:
self.config = load(f)
if not os.path.exists('./images'):
os.mkdir('./images')
print('请将需要优化的图片放置在images目录中')
sleep(5)
else:
image = os.listdir('./images')
self.image = [i for i in image if not i.startswith('new')]
def beautify_image(self):
os.startfile('images')
for image in self.image:
#try:
if not image.startswith('new'):
r = post(url='https://api-cn.faceplusplus.com/facepp/v2/beautify',
data=self.config,
files={
'image_file': (image, open(os.path.join('./images', image), 'rb'), 'image/jpeg')})
print(r.json())
with open(os.path.join('./images', 'new_' + image), 'wb') as f:
f.write(base64.b64decode(r.json()['result']))
sleep(3)
# except Exception as e:
# print(e)
# print('处理异常')
# sleep(5)
if __name__ == '__main__':
ai = Kuangshi()
ai.beautify_image()