python,如何保存tkinter图像?

时间:2016-01-04 14:20:04

标签: image python-2.7 tkinter tkinter-canvas

我找到了一个打开典型"另存为" - 窗口的代码,但是如果我想将我的tkinter图像保存为例如.jpg,它只保存没有图像的.jpg文件。其他选项也有效。

示例代码:

### this class will open a safe-file-as standart menu
class TkFileDialogExample(Tkinter.Frame):

    def __init__(self, root):

        Tkinter.Frame.__init__(self, root)
        button_opt = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5}
        Tkinter.Button(self, text='asksaveasfilename', \
                       command = self.asksaveasfilename).pack(**button_opt)


        self.file_opt = options = {}
        options['filetypes'] = [('all files', '.*'), ('text files', '.txt'),
                                ('jpeg image', '.jpg'), ('png image', '.png'),
                                ('tiff image', '.tiff'), ('bmp image', '.bmp')]

        options['initialfile'] = 'myfile.txt'
        options['parent'] = root

    def asksaveasfilename(self):
        filename = tkFileDialog.asksaveasfilename(**self.file_opt)

        if filename:
            return image.save(filename)

这是我的"在tkinter打开图像"代码:

##this  function will open an image in a canvas, but the functions
   #expects a picture. It also converts a image into a TKinter photo image#
def imgShow(master, img):

    #convert lena.jpg into tkinter photo image
    global image 
    image = Image.open(img)
    photo = ImageTk.PhotoImage(image)

    #create canvas to display picture
    w = Canvas(master)
    w.photo = photo


    w.create_image(0, 0, image = photo, anchor = "nw")
    w.pack(fill = BOTH, expand = YES)

    return image

也许您知道如何保存图片。

0 个答案:

没有答案