Can I save a tkinter PhotoImage as .gif (or any other image extension) using only Python and tkinter?
            Asked
            
        
        
            Active
            
        
            Viewed 5,299 times
        
    3
            
            
        - 
                    If you have a PhotoImage created like `photo = PhotoImage(file="image.gif")`, then the image already exists on your hard drive as `image.gif`. No saving required. – Kevin Aug 19 '14 at 15:58
 - 
                    I create PhotoImage without specifying an image, i fill it myself. – Ilya Peterov Aug 19 '14 at 16:03
 - 
                    Oh, ok. I didn't know you could do that :-) – Kevin Aug 19 '14 at 16:05
 
2 Answers
3
            Assuming photo is the variable holding your image, you should be able to just say:
photo.write('some_name.gif', format='gif')
Although it doesn't say explicitly, it supports viewing PGM, PPM, GIF, PNG formats, so I assume it can save to these formats as well.
        Gerrat
        
- 28,863
 - 9
 - 73
 - 101
 
- 
                    .png format works as well. There are problems with transparency, thouhgh:( – Ilya Peterov Aug 19 '14 at 17:06
 - 
                    Good to know. I actually tried & failed with `.png`, but likely had an issue elsewhere. – Gerrat Aug 19 '14 at 17:11
 - 
                    
 - 
                    
 - 
                    What I'm needing is the transparency from a png, but I can't find any help in this area. I'm loading a png image as PhotoImage, and then turning around and saving the same image as a different file. The resulting image seems to skip whole lines and invert the transparency on other lines. – Veggiet Apr 15 '21 at 16:52
 
2
            
            
        Looks like the method write can be used to save a PhotoImage to a file.
>>> help(Tkinter.PhotoImage.write)
Help on method write in module Tkinter:
write(self, filename, format=None, from_coords=None) unbound Tkinter.PhotoImage method
    Write image to file FILENAME in FORMAT starting from
    position FROM_COORDS.
Sample usage would be something like:
my_photo_image.write("output.gif")
        Kevin
        
- 74,910
 - 12
 - 133
 - 166