题目要求:设计GUI,采用对话框选择目录,获取所有.raw图片,并可以采用键盘p键和n键分别向前和向后依次显示该目录下所有.raw图片
现有代码:
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
import os
import numpy
import matplotlib
matplotlib.use('TkAgg') # this must be in front of any matplotlib or pylab calls
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import sys
sys.path.insert(0,"C:\Users\11520\Desktop\数字图像处理\bb\bb")
from raw_file_access import read_raw
def list_all_raw_files(dirname):
#print(len(os.listdir(dirname)))
files = [e for e in os.listdir(dirname) if e.upper().endswith('.RAW')]
#print(files)
return [os.path.join(dirname, e) for e in files]
def histogram(image):
img = image.ravel()
#print(image.shape, img.shape)
hist = [0]*256
for i in range(img.size):
hist[img[i]] += 1
return hist
class main_window(tk.Frame):
def init(self, master = None):
tk.Frame.init(self, master)
self.master = master
self.pack(fill=tk.BOTH, expand=1)
self.image_file_list = []
self.image_index = None
#place widgets
file_button = tk.Button(self, text="open a file", command = self.on_fileopen)
file_button.place(x=10, y = 0)
dir_button = tk.Button(self, text="open a directory", command = self.on_diropen)
dir_button.place(x=100, y = 0)
main_image_label = tk.Label(self, text="image placehold")
main_image_label.place(x=0, y=40)
self.main_image_label = main_image_label
self.main_image_label.bind('<Button-1>', self.image_forward)
self.main_image_label.bind('<Button-3>', self.image_backward)
f = Figure((3.8,3))
ax = f.add_subplot(1,1,1)
self.f = f
self.ax = ax
self.canvas = FigureCanvasTkAgg(f, master=root)
#self.canvas.draw()
self.canvas.get_tk_widget().place(x=4, y=440)
def on_fileopen(self):
filename = filedialog.askopenfilename()
if filename != "":
self.image_file_list = [filename]
self.image_index = 0
self.show_image()
def on_diropen(self):
dirname = filedialog.askdirectory()
if dirname != "":
self.image_file_list = list_all_raw_files(dirname)
self.image_index = 0
self.show_image()
def show_image(self):
filename = self.image_file_list[self.image_index]
img = read_raw(filename)
org_image_obj = Image.fromarray(img)
org_image_tk = ImageTk.PhotoImage(org_image_obj)
self.main_image_label.configure(image=org_image_tk)
self.main_image_label.image = org_image_tk
hist = histogram(img)
self.ax.cla()
self.ax.plot(hist)
self.canvas.draw()
def image_forward(self, event):
self.image_index += 1
if self.image_index == len(self.image_file_list):
self.image_index = 0
self.show_image()
def image_backward(self, event):
self.image_index -= 1
if self.image_index == -1:
self.image_index = len(self.image_file_list)-1
self.show_image()
if name == "main":
root = tk.Tk()
app = main_window(root)
root.wm_title("Image display window")
root.geometry("450x750")
root.mainloop()
老师给的 多了灰度直方图显示,并且切换用的鼠标左右键
代码中这两行:
self.main_image_label.bind('', self.image_forward)
self.main_image_label.bind('', self.image_backward)
改一下键盘按键绑定:
self.main_image_label.bind('<Key-p>', self.image_forward)
self.main_image_label.bind('<Key-n>', self.image_backward)