หลักการก็คือต้องสร้างเมนู Save as... จากนั้นก็เพิ่มโค้ดในส่วนของการบันทึกรูป โค้ดทั้งหมดก็จะเป็นตามนี้ครับ
from Tkinter import Tk,Menu,Label from PIL import Image,ImageTk import tkFileDialog class MainApp: #constructor def __init__(self,mainwindow): #assign class variable to input parameter self.mainwindow = mainwindow #set title self.mainwindow.title("Menu") self.lbl = Label(self.mainwindow) def createMenu(self): #create menubar menubar = Menu(self.mainwindow) self.mainwindow.config(menu=menubar) #FILE menu fileMenu = Menu(menubar, tearoff=0) menubar.add_cascade(label="File", menu=fileMenu) fileMenu.add_command(label="Open...",command=self.openFile) fileMenu.add_command(label="Save as...",command=self.saveFile) fileMenu.add_separator() fileMenu.add_command(label="Exit", command=self.mainwindow.destroy) #EDIT menu editMenu = Menu(menubar, tearoff=0) menubar.add_cascade(label="Edit", menu=editMenu) editMenu.add_command(label="Restore Image", command=self.imgRestore) #PROCESS menu processMenu = Menu(menubar, tearoff=0) menubar.add_cascade(label="Process", menu=processMenu) processMenu.add_command(label="Rotate", command=self.imgRotate) def openFile(self): #file type filter ftypes = [('Image files', '*.jpg *.png *.gif'), ('All files', '*')] filename = tkFileDialog.askopenfilename(parent=self.mainwindow,title='Choose a file',filetypes=ftypes) if filename: try: #open image self.im = Image.open(filename) #keep its copy for restoring later self.imOrigin = self.im.copy() self.showImage() except: pass def saveFile(self): ftypes = [('JPEG', '*.jpg')] filename = tkFileDialog.asksaveasfilename(parent=self.mainwindow,title='Save image as',filetypes=ftypes) if len(filename)>0: try: #in Linux, comment this line, it is not useful filename = filename+".jpg" #save image self.im.save(filename,"JPEG") except IOError: tkMessageBox.showerror("Error","Cannot save image!") def showImage(self): self.imTk = ImageTk.PhotoImage(self.im) self.lbl.destroy() self.lbl = Label(self.mainwindow, image=self.imTk) self.lbl.pack() def imgRestore(self): self.im = self.imOrigin.copy() self.showImage() def imgRotate(self): self.im = self.im.rotate(45) self.showImage() #---Main app starts here--- root = Tk() app = MainApp(root) #create menu app.createMenu() root.mainloop()