Open In App

Convert files from jpg to gif and vice versa using Python

Last Updated : 26 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 convert the image From JPG to GIF : {Syntax}

img = Image.open("Image.jpg")
img.save("Image.gif")

Step 3: GIF to JPG

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

Adding the GUI interface

from tkinter import *

Approach:

  • 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 Convert 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:

Python3




# 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
root.title("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 = Image.open(import_filename)
   
        # after converting the image save to desired
        # location with the Extersion .png
        export_filename = fd.asksaveasfilename(defaultextension=".gif")
        im1.save(export_filename)
   
        # displaying the Messaging box with the Success
        messagebox.showinfo("success ", "your Image converted to GIF Format")
    else:
   
        # 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))
        Label_2.place(x=80, 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"):
            im1=Image.open(import_filename).convert('RGB')
            export_filename=fd.asksaveasfilename(defaultextension=".jpg")
            im1.save(export_filename)
            messagebox.showinfo("Success","File converted to .jpg")
             
    else:
        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)
   
button1.place(x=120, 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)
   
button2.place(x=120, y=220)
 
# adjust window size
root.geometry("500x500+400+200")
root.mainloop()




Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads