Site icon DataFlair

Python Image Format Converter – Convert JPG to PNG

Python course with 57 real-time projects - Learn Python

Many times we find the need to change the format of an image. In this project, we are going to create an Image Format Converter using Python which will help us to change the extension of an image from .png to .jpg and vice versa. So without wasting any more time, let’s start with the project.

What is an Image Format Converter?

An Image Format Converter is an application that helps a user convert the format/extension of an image. Here we are providing two image extensions – png and jpg. This project will have a GUI Window which will provide both of these options.

Project Details

To create the GUI of this project, we are going to use the Tkinter Module. Tkinter Module has a number of inbuilt libraries that will help us to create GUI very easily. To convert images into different formats, we will be using the PIL Module. The GUI window has two conversion options-

Project Prerequisites

To proceed with the project we will need to install the following libraries with the given command:

Download Python Image Format Converter Code

Please download the source code for python image format converter project from the following link: Python Image Format Converter Project

Steps to Create the Image Format Converter Project

We will be performing the following steps to develop this python project-

  1. Importing Libraries and Modules
  2. Browsing an image file from the system.
  3. Converting from Png to Jpg.
  4. Converting from Jpg to Png.
  5. Creating the GUI Window.

Let’s have a look at each step in detail.

1. Importing Libraries and Modules:

#importing libraries and modules
import tkinter as tk
from tkinter import *
from tkinter import filedialog
from PIL import Image

2. Browsing an Image File from the System:

 
#function to browse image
def browse():
    global img
    filename = filedialog.askopenfilename(title = "Select a File")#selecting a file from the system
    img = Image.open(filename)#opening the selected file

3. Convert from Png to Jpg:

 
#function to change from png to jpg
def png_to_jpg():
    global img
    export_file_path = filedialog.asksaveasfilename(defaultextension='.jpg')#choosing the path and changing extension to jpg
    img.save(export_file_path)#saving the file on desired path

4. Convert From Jpg to Png:

#function to change from jpg to png
def jpg_to_png():
    global img
    export_file_path = filedialog.asksaveasfilename(defaultextension='.png')#choosing the path and changing extension to jpg
    img.save(export_file_path)#saving the file on desired path

5. Creating the GUI Window:

#creating window
root = Tk()
root.geometry('600x250')#geometry of window
root.title('DataFlair')#title for window
Label(root,text='Image Format Converter',font='arial 15').place(x=210,y=10)
Button(root,text='Browse an Image',command=browse,fg='blue',font='arial 10').place(x=250,y=45)#creating button
Button(root,text='Png To Jpg',command=png_to_jpg,fg='red',font='arial 10').place(x=120,y=95)
Button(root,text='Jpg To Png',command=jpg_to_png,fg='red',font='arial 10').place(x=450,y=95)
root.mainloop()

Python Image Format Converter Output

Summary

In this project, we have created an Image Format Converter using Python. Here we have used the following two libraries to build the project.

I hope you have enjoyed building this project with DataFlair.

Exit mobile version