欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python实战练习之最新男女颜值打分小系统

程序员文章站 2022-03-28 20:06:02
导语​​哈喽!我是木木子,今天又想我了嘛?之前不是出过一期python美颜相机嘛?不知道你们还记得不?这一期的话话题还是围绕上期关于颜值方面来走。还是原来的配方,还是原来的味道...

导语​

python实战练习之最新男女颜值打分小系统

哈喽!我是木木子,今天又想我了嘛?

之前不是出过一期python美颜相机嘛?不知道你们还记得不?这一期的话话题还是围绕上期关于颜值方面来走。

还是原来的配方,还是原来的味道。

偶尔有女生或者说男生都有过这样的经历,偶然照镜子的时候觉得自己美、帅到爆炸。【小编打死不会承认的.jpg】

python实战练习之最新男女颜值打分小系统

但打开无美颜的前置摄像头无滤镜,或者看到真正的漂亮小姐姐,又会感慨自己怎么能这么丑!

python实战练习之最新男女颜值打分小系统

颜值打分其实是个很有争议,并且人人都感兴趣的话题,那么今天木木子就带着python颜值打分神器走来了!

如果满分100分,平均分60,你会给自己的颜值打几分?

正文

本文是基于tkinter做的界面化颜值打分小系统哈,快来测测你的颜值打多少分呀~

环境安装部分:python3、pycharm2021、以及一些自带的模块。

pip install -i https://pypi.douban.com/simple/ pillow 
pip install -i https://pypi.douban.com/simple/ baidu-aip

​首先还是肯定配置百度api参数如下:

app_id = '15768642'
api_key = 'xhiigmgprcrj10xiqvlvecky'
secret_key = 'zdmmao7stwtkzw8bspvqxvogtdgsw4yi'
a_face = aipface(app_id, api_key, secret_key)
image_type = 'base64'
options = {'face_field': 'age,gender,beauty'}

标题设计颜色、字体等:

    def title(self):
        """标题设计"""
        lb = tk.label(self.root, text='颜值打分系统',
                      bg='#008b8b',
                      fg='lightpink', font=('楷书', 30),
                      width=20,
                      height=2,
                      # relief=tk.sunken
                      )
        lb.place(x=200, y=10)

设置了界面化程序的背景大小等:

class scoresystem():
 
    root = tk.tk()
 
    # 修改程序框的大小
    root.geometry('800x500')
 
    # 添加程序框标题
    root.title('颜值打分系统')
 
    # 修改背景色
    canvas = tk.canvas(root,
                       width=800,  # 指定canvas组件的宽度
                       height=500,  # 指定canvas组件的高度
                       bg='#e6e8fa')  # 指定canvas组件的背景色
    canvas.pack()

主函数运行:

 def start_interface(self):
        """主运行函数"""
        self.title()
        self.time_component()
 
        # 打开本地文件
        tk.button(self.root, text='打开文件', command=self.show_original_pic).place(x=50, y=150)
        # 进行颜值评分
        tk.button(self.root, text='颜值识别', command=self.open_files2).place(x=50, y=230)
 
        # 退出系统
        tk.button(self.root, text='退出软件', command=self.quit).place(x=50, y=390)
        # 显示图框标题
        tk.label(self.root, text='原图', font=10).place(x=380, y=120)
        # 修改图片大小
        self.label_img_original = tk.label(self.root)
        # 设置显示图框背景
        self.cv_orinial = tk.canvas(self.root, bg='white', width=270, height=270)
        # 设置显示图框边框
        self.cv_orinial.create_rectangle(8, 8, 260, 260, width=1, outline='red')
        # 设置位置
        self.cv_orinial.place(x=265, y=150)
        # 显示图片位置
        self.label_img_original.place(x=265, y=150)
 
        # 设置评分标签
        tk.label(self.root, text='性别', font=10).place(x=680, y=150)
        self.text1 = tk.text(self.root, width=10, height=2)
        tk.label(self.root, text='年龄', font=10).place(x=680, y=250)
        self.text2 = tk.text(self.root, width=10, height=2)
        tk.label(self.root, text='评分', font=10).place(x=680, y=350)
        self.text3 = tk.text(self.root, width=10, height=2)
 
        # 填装文字
        self.text1.place(x=680, y=175)
        self.text2.place(x=680, y=285)
        self.text3.place(x=680, y=385)
 
        # 开启循环
        self.root.mainloop()
 
    def show_original_pic(self):
        """放入文件"""
        self.path_ = askopenfilename(title='选择文件')
        # 处理文件
        img = image.open(fr'{self.path_}')
        img = img.resize((270, 270), pil.image.antialias)  # 调整图片大小至270*270
        # 生成tkinter图片对象
        img_jpg_original = imagetk.photoimage(img)
        # 设置图片对象
        self.label_img_original.config(image=img_jpg_original)
        self.label_img_original.image = img_jpg_original
        self.cv_orinial.create_image(5, 5, anchor='nw', image=img_jpg_original)
 
    def open_files2(self):
        # 获取百度api接口获得的年龄、分数、性别
        age, score, gender = face_score(self.path_)
 
        # 清楚text文本框内容并进行插入
        self.text1.delete(1.0, tk.end)
        self.text1.tag_config('red', foreground='red')
        self.text1.insert(tk.end, gender, 'red')
 
        self.text2.delete(1.0, tk.end)
        self.text2.tag_config('red', foreground='red')
        self.text2.insert(tk.end, age, 'red')
 
        self.text3.delete(1.0, tk.end)
        self.text3.tag_config('red', foreground='red')
        self.text3.insert(tk.end, score, 'red')
 
 
 
    def quit(self):
        """退出"""
        self.root.quit()

最后还​设置了时间组,随时更新测试颜值的时间,就可以测出不同时间段颜值。

  def get_time(self, lb):
        """获取时间"""
        time_str = time.strftime("%y-%m-%d %h:%m:%s")  # 获取当前的时间并转化为字符串
        lb.configure(text=time_str)  # 重新设置标签文本
        self.root.after(1000, self.get_time, lb)  # 每隔1s调用函数 get_time自身获取时间
 
    def time_component(self):
        """时间组件"""
        lb = tk.label(self.root, text='', fg='white', font=("黑体", 15))
        lb.place(relx=0.75, rely=0.90)
        self.get_time(lb)

效果如下:

python实战练习之最新男女颜值打分小系统

python实战练习之最新男女颜值打分小系统

python实战练习之最新男女颜值打分小系统

​嘿嘿!仅仅供大家学习娱乐交流的~很多颜值打分不准滴!请轻点儿捶我.jpg。

总结

好啦!文章就写到这里,这款颜值打分神器需要的小小伙伴儿自取!

mua~ 你们的支持是我最大的动力!!

python实战练习之最新男女颜值打分小系统

到此这篇关于python实战练习之最新男女颜值打分小系统的文章就介绍到这了,更多相关python 男女颜值打分系统内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!