導語?
?
哈嘍!我是木木子,今天又想我了嘛?
之前不是出過一期Python美顏相機嘛?不知道你們還記得不?這一期的話話題還是圍繞上期關于顏值方面來走,
還是原來的配方,還是原來的味道,
偶爾有女生或者說男生都有過這樣的經歷,偶然照鏡子的時候覺得自己美、帥到爆炸,【小編打死不會承認的.jpg】
?
但打開無美顏的前置攝像頭無濾鏡,或者看到真正的漂亮小姐姐,又會感慨自己怎么能這么丑!
?
顏值打分其實是個很有爭議,并且人人都感興趣的話題,那么今天木木子就帶著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_png_original = ImageTk.PhotoImage(img)
# 設定圖片物件
self.label_img_original.config(image=img_png_original)
self.label_img_original.image = img_png_original
self.cv_orinial.create_image(5, 5, anchor='nw', image=img_png_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)
效果如下:



?嘿嘿!僅僅供大家學習娛樂交流的~很多顏值打分不準滴!請輕點兒捶我.jpg,
總結
好啦!文章就寫到這里,這款顏值打分神器需要的小小伙伴兒自取!
如需完整的專案原始碼+素材原始碼基地見:#私信小編06#或者點擊藍色文字添加即可獲取免費的福利!
記得三連哦~mua 你們的支持是我最大的動力!!

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/303316.html
標籤:python
下一篇:資料集:大學畢業生收入
