Converting files from jpg to gif using Python
Converting a jpg file to a gif file using Python
Sometimes, when attaching an image, we need to specify an image extension. We can have images with different extensions and need to convert them to a specific extension. For example, here we will convert an image with a .jpg extension to a .gif extension, and vice versa.
In addition, we will create a GUI interface for our code, so we will need the tkinter library. Tkinter is a Python binding for the Tk GUI toolkit. It is the standard Python interface for the Tk GUI toolkit, providing an interface for GUI applications.
Follow the steps below:
Step 1:Import the library.
from PIL import Image
Step 2: Convert JPG to GIF
To convert the image From JPG to GIF : {Syntax}
img = Image.open("Image.jpg")
img.save("Image.gif")
Step 3: Convert GIF to JPG
To convert the Image From PNG to JPG
img = Image.open("Image.gif")
img.save("Image.jpg")
Add GUI interface
from tkinter import *
Steps:
- In the jpg_to_gif() function, we first check if the selected image is the same format (.jpg) that we want to convert to .gif. If not, we return an error.
- Otherwise, we convert the image to .gif.
- To open the image, we use the FileDialog function in tkinter to help us open images from a folder.
- Import filedialog from tkinter as fd.
- The GIF to JPG conversion method is the same.
The following 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
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
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")
#DriverCode
# 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()