Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Convert files from jpg to png and vice versa using Python

  • Difficulty Level : Medium
  • Last Updated : 21 Sep, 2021

Prerequisite: Pillow Library 

Sometime 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 specified extension like in this we will convert the image having Extension of PNG to JPG 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, and is Python’s de facto standard GUI. 

Follow the below Steps:

Step 1: Import the library.

from PIL import Image

Step 2: JPG to PNG

To convert the image From JPG to PNG : {Syntax}

img ="Image.jpg")"Image.png")

Step 3: PNG → JPG

To convert the Image From PNG to JPG

img ="Image.png")"Image.jpg")

Adding the GUI interface

from tkinter import *


  1. In Function jpg_to_png we first Check whether The Selecting the image is in the same Format (.jpg) which to convert to .png if not then return Error.
  2. Else Convert the image the to .png
  3. 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
  4. Same Approach for the PNG to JPG

Below is the Implementation:


# 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
# 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 =
        # after converting the image save to desired
        # location with the Extersion .png
        export_filename = fd.asksaveasfilename(defaultextension=".png")
        # displaying the Messaging box with the Success
        messagebox.showinfo("success ", "your Image converted to Png")
        # 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...")
def png_to_jpg():
    global im1
    import_filename = fd.askopenfilename()
    if import_filename.endswith(".png"):
        im1 =
        export_filename = fd.asksaveasfilename(defaultextension=".jpg")
        messagebox.showinfo("success ", "your Image converted to jpg ")
        Label_2 = Label(root, text="Error!", width=20,
                        fg="red", font=("bold", 15)), 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), 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), y=220)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!