Skip to content
Related Articles

Related Articles

Convert files from jpg to gif and vice versa using Python
  • Last Updated : 24 Jan, 2021
GeeksforGeeks - Summer Carnival Banner

Sometimes it is required to attach the Image where we required image file with the specified extension. And we have the image with the different extension which needs to be converted with a specified extension like in this we will convert the image having Extension of .jpg to .gif and Vice-Versa

And Also we will be creating the GUI interface to the code, so we will require the Library tkinter. Tkinter 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

Follow the below steps:

Step 1: Import the library.

from PIL import Image

Step 2: JPG to GIF

To covert the image From JPG to GIF : {Syntax}

img ="Image.jpg")"Image.gif")

Step 3: GIF to JPG

To convert the Image From PNG to JPG
img ="Image.gif")"Image.jpg")

Adding the GUI interface

from tkinter import *


  • In function jpg_to_gif() we first check whether The Selecting the image is in the same format (.jpg) which to convert to .gif if not then return error.
  • Else Covert the image the to .gif.
  • To open the Image we use the Function in tkinter called the FileDialog which helps to open the image from the folder.
  • From tkinter import filedialog as fd.
  • Same Approach for the GIF to JPG.

Below is the Implementation:


# import required modules
from tkinter import *
from tkinter import filedialog as fd 
import os 
from PIL import Image 
from tkinter import messagebox 
# create TK object  
root = Tk() 
# naming the GUI interface to image_conversion_APP 
# function to convert jpg to gif
def jpg_to_gif(): 
    global im1 
    # import the image from the folder 
    import_filename = fd.askopenfilename() 
    if import_filename.endswith(".jpg"): 
        im1 = 
        # after converting the image save to desired 
        # location with the Extersion .png 
        export_filename = fd.asksaveasfilename(defaultextension=".gif" 
        # displaying the Messaging box with the Sucess 
        messagebox.showinfo("success ", "your Image converted to GIF Format"
        # if Image select is not with the Format of .jpg  
        # then display the Error 
        Label_2 = Label(root, text="Error!", width=20
                        fg="red", font=("bold", 15)), y=280
        messagebox.showerror("Fail!!", "Something Went Wrong..."
# function to convert gif to jpg  
def gif_to_jpg(): 
    global im1 
    import_filename = fd.askopenfilename() 
    if import_filename.endswith(".gif"):
            messagebox.showinfo("Success","File converted to .jpg")
        messagebox.showerror("Fail!!","Error Interrupted!!!! Check Again")
# Driver Code
# add buttons
button1 = Button(root, text="JPG_to_GIF", width=20
                 height=2, bg="green", fg="white"
                 font=("helvetica", 12, "bold"), 
                 command=jpg_to_gif), y=120
button2 = Button(root, text="GIF_to_JPG", width=20
                 height=2, bg="green", fg="white"
                 font=("helvetica", 12, "bold"), 
                 command=gif_to_jpg), y=220
# adjust window size

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :