需要在嵌入式设备上全屏显示图像,使用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了