Skip to content
Related Articles

Related Articles

Convert the .PNG to .GIF and it’s vice-versa in Python
  • Last Updated : 24 Jan, 2021


Python supports subsystems for converting one file format to another. This article discusses this topic and depicts how a png file can be converted to its gif equivalent and vice versa. For conversion of one file format to the other PIL is employed.

The given example uses a GUI interface to the code, so we will require Tkinter. It is a Python binding to the Tk GUI toolkit. It is the standard Python interface to the Tk GUI toolkit which provide the interface to the GUI apps.


  • Import modules
  • Create a normal window
  • Add buttons to take choice whether to convert to png to gif or vice versa
  • Open file
  • Check if the file supplied is of the correct format
  • Convert to its respective equivalent
  • Save image
  • Execute code



from tkinter import *
from tkinter import filedialog as fd
import os
from PIL import Image
from tkinter import messagebox
root = Tk()
# naming the GUI interface to image_conversion_APP
# creating the Function which converts the jpg_to_png
def gif_to_png():
    global im
    import_filename = fd.askopenfilename()
    if import_filename.endswith(".gif"):
        im =
        export_filename = fd.asksaveasfilename(defaultextension=".png")
        messagebox.showinfo("Success", "File converted to .png")
        messagebox.showerror("Fail!!", "Error Interrupted!!!! Check Again")
def png_to_gif():
    import_filename = fd.askopenfilename()
    if import_filename.endswith(".png"):
        im =
        export_filename = fd.asksaveasfilename(defaultextension=".gif")
        messagebox.showinfo("Success", "File converted to .gif")
        messagebox.showerror("Fail!!", "Error Interrupted!!!! Check Again")
button1 = Button(root, text="GIF_to_PNG", width=20, height=2, bg="green",
                 fg="white", font=("helvetica", 12, "bold"), command=gif_to_png), y=120)
button2 = Button(root, text="PNG_to_GIF", width=20, height=2, bg="green",
                 fg="white", font=("helvetica", 12, "bold"), command=png_to_gif), y=220)


 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :