PIL(Python Image Library)是python的第三方圖像處理庫,但是由於其強大的功能與衆多的使用人數,幾乎已經被認爲是python官方圖像處理庫了。其官方主頁爲:PIL。 PIL歷史悠久,原來是隻支持python2.x的版本的,後來出現了移植到python3的庫pillow,pillow號稱是friendly fork for PIL,其功能和PIL差不多,但是支持python3。本
PIL可以做很多和圖像處理相關的事情:
圖像歸檔(Image Archives)。PIL非常適合於圖像歸檔以及圖像的批處理任務。你可以使用PIL創建縮略圖,轉換圖像格式,打印圖像等等。
圖像展示(Image Display)。PIL較新的版本支持包括Tk PhotoImage,BitmapImage還有Windows DIB等接口。PIL支持衆多的GUI框架接口,可以用於圖像展示。
圖像處理(Image Processing)。PIL包括了基礎的圖像處理函數,包括對點的處理,使用衆多的卷積核(convolution kernels)做過濾(filter),還有顏色空間的轉換。PIL庫同樣支持圖像的大小轉換,圖像旋轉,以及任意的仿射變換。PIL還有一些直方圖的方法,允許你展示圖像的一些統計特性。這個可以用來實現圖像的自動對比度增強,還有全局的統計分析等
環境:WINDOWS
編程工具:pycharm
所需組件: PIL ,Baidu-Api(人臉識別模塊)
(1)準備組件
A.查看當前版本
pip --version
B.升級pip版本
python -m pip install --index-url https://pypi.douban.com/simple --upgrade pip
C. 安裝baidu-api
D.在pycharm界面setting設置安裝pillow模塊(pil至python3.0版本都已經歸併爲pillow)
(2)代碼編寫
import PIL import time import base64 import tkinter as tk from PIL import Image from PIL import ImageTk from aip import AipFace from tkinter.filedialog import askopenfilename # 配置百度aip參數 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 get_file_content(file_path): """獲取文件內容""" with open(file_path, 'rb') as fr: content = base64.b64encode(fr.read()) return content.decode('utf8') def face_score(file_path): """臉部識別分數""" result = a_face.detect(get_file_content(file_path), image_type, options) print(result) age = result['result']['face_list'][0]['age'] beauty = result['result']['face_list'][0]['beauty'] gender = result['result']['face_list'][0]['gender']['type'] return age, beauty, gender 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='blue', font=("黑體", 15)) lb.place(relx=0.75, rely=0.90) self.get_time(lb) def title(self): """標題設計""" lb = tk.Label(self.root, text='顏值打分系統', bg='#6495ED', fg='lightpink', font=('宋體', 32), width=20, height=2, # relief=tk.SUNKEN ) lb.place(x=200, y=10) score_system = ScoreSystem() score_system.start_interface()
運行結果: