需要在嵌入式設備上全屏顯示圖像,使用pil顯示圖像時,只能通過系統的圖像瀏覽器顯示。所以使用Python自帶的tkinter
import Tkinter as tk 這句在Python3中已經改成了 import tkinter as tk
1 top = Tk() #導入tk模塊 2 from PIL import Image, ImageTk 3 image = Image.open("lenna.jpg") 4 photo = ImageTk.PhotoImage(image) 5 label = Label(top) 6 label.pack() 7 label.configure(image = photo ) 8 top.mainloop()
在Ubuntu下需要安裝:
sudo apt-get install python3-tk
sudo apt-get install python3-pil.imagetk
win下無需安裝
使用PIL的原因是tkinter自己無法顯示jpg圖像
功能驗證代碼:
1 #!/usr/bin/python3 2 #!/usr/local/bin/python3 3 4 from tkinter import * 5 from PIL import Image, ImageTk 6 7 top = Tk() #導入tk模塊 8 top.attributes("-fullscreen", True) 9 width=top.winfo_screenwidth() 10 height=top.winfo_screenheight() 11 print(width,height) 12 image = Image.open('d:/cur/proj/j20/3.jpg') 13 photo = ImageTk.PhotoImage(image.resize((width,height))) 14 label = Label(top) 15 label.pack(expand=YES,fill=BOTH) #讓圖像在中央填充 16 label.configure(image = photo ) 17 top.mainloop()
完成功能驗證后,編寫了刷圖像的程序:
1 def display_thread(): 2 global label,top 3 while True: 4 t1=time.time() 5 image = Image.open('d:/cur/proj/j20/3.jpg') 6 photo = ImageTk.PhotoImage(image.resize((width,height))) 7 #photo = ImageTk.PhotoImage(image) 8 label.configure(image = photo ) 9 #top.update_idletasks() 10 time.sleep(0.03) 11 t2=time.time() 12 print(t2-t1)
發現圖像顯示出現了閃屏,而且幀率只有5~10幀,CPU占用率已達到單核90%
而實測image = Image.open('d:/cur/proj/j20/3.jpg')
僅需1ms,說明imagetk消耗時間長,無法滿足30幀每秒的需求。
尚不知如何優化,換pyqt了
