Search code examples
pythontkintersavefiledialogfiledialog

filedialog, python, save as


i create a very simple photo editor. I've alredy create open-file and save-file button, and command to open file:

def Open(self):
        #Open Callback
        ftypes = [('Image Files', '*.tif *.jpg *.png')]
        dlg = filedialog.Open(self, filetypes = ftypes)
        filename = dlg.show()
        self.fn = filename
        self.setImage()

Now I want to create save command and I'm stuck:

 def save(self):
        myFormats = [('Windows Bitmap','*.bmp'),\
                     ('Portable Network Graphics','*.png'),\
                     ('JPEG / JFIF','*.jpg'),('CompuServer GIF','*.gif'),]
        filename = filedialog.asksaveasfilename()

How to use this formats as formats my saving image?


Solution

  • To include those formats use filetypes option:

    filename = filedialog.asksaveasfilename(filetypes=myFormats)
    
    if filename:
        #do save
    

    For example if I open an image like this:

    import Image
    image = Image.open(filename)
    

    To save I have just to do:

    image.save(filename)