Converting files from jpg to png using Python
Converting a File from jpg to png Using Python
Prerequisite: Pillow Library
Sometimes, when attaching an image, we need an image file with a specific extension. We have an image with a different extension and need to convert it to a specific extension. For example, here we will convert an image with a PNG extension to a JPG, 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 to the Tk GUI toolkit and the de facto standard GUI for Python.
Convert files from JPG to PNG using Python
from PIL import Image
# Adding the GUI interface
from tkinter import *
# To convert the image From JPG to PNG : {Syntax}
img = Image.open("Image.jpg")
img.save("Image.png")
# To convert the Image From PNG to JPG
img = Image.open("Image.png")
img.save("Image.jpg")
A Wizard for Converting JPG to PNG Using Python
Steps:
- In the JPG_to_png function, we first check if the selected image is in the same format (.jpg) and then convert it to .png. If not, an error is returned.
- Otherwise, the image is converted to .png.
- To open the image, we use a Tkinter function called FileDialog, which helps open images from a folder.
from tkinter import filedialog as fd - The method of converting PNG to JPG is the same
# import all prerequisite
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
root.title("Image_Conversion_App")
# creating the Function which converts the jpg_to_png
def jpg_to_png():
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=".png")
im1.save(export_filename)
# displaying the Messaging box with the Success
messagebox.showinfo("success ", "your Image converted to Png")
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...")
def png_to_jpg():
global im1
Import_filename = fd.askopenfilename()
If import_filename.endswith(".png"):
im1 = Image.open(import_filename)
Export_filename = fd.asksaveasfilename(defaultextension=".jpg")
im1.save(export_filename)
messagebox.showinfo("success ", "your Image converted to jpg ")
else:
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...")
button1 = Button(root, text="JPG_to_PNG", width=20, height=2, bg="green",
fg="white", font=("helvetica", 12, "bold"), command=jpg_to_png)
button1.place(x=120, y=120)
button2 = Button(root, text="PNG_to_JPEG", width=20, height=2, bg="green",
fg="white", font=("helvetica", 12, "bold"), command=png_to_jpg)
button2.place(x=120, y=220)
root.geometry("500x500+400+200")
root.mainloop()
Output:
Convert files from jpg to png