Home Python Project Image Converter GUI with Python

Image Converter GUI with Python

by Adarsh Pal
22 minutes read

There are various image file formats, like JPG and PNG. In this article, I’ll show you how to create a Python GUI to convert PNG to JPG.

Image Converter GUI with Python

To create an Image converter GUI with Python, I will use the Tkinter library in Python which is the best-known Python framework for building GUI application. Other than Tkinter we also need PIL library in Python which stands for Python Imaging Library.

Now let’s see how to create an application to convert a PNG image to JPG:

import tkinter as tk
from tkinter import filedialog
from PIL import Image

root = tk.Tk()
canvas1 = tk.Canvas(root, width=300, height=250, bg='azure3', relief='raised')
canvas1.pack()

label1 = tk.Label(root, text="Image Converter", bg='azure3')
label1.config(font=('helvetica', 20))
canvas1.create_window(150, 60, window=label1)

def getPNG():
    global im1
    import_file_path = filedialog.askopenfilename()
    im1 = Image.open(import_file_path)

browse_png = tk.Button(text="Select PNG file", command=getPNG, bg="royalblue", fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 130, window=browse_png)

def convert():
    global im1
    export_file_path = filedialog.asksaveasfilename(defaultextension='.jpg')
    im1.save(export_file_path)

saveasbutton = tk.Button(text="Convert PNG to JPG", command=convert, bg='royalblue', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 180, window=saveasbutton)
root.mainloop()

As you can see the output window, it will work the same as all other applications installed in your system. You first need to select a PNG file then click on the button to convert PNG to JPG, then you just need to select a folder where you want to save your converted image. 

This is how we can easily create a GUI application to convert Images. I hope you liked this article on how to build an Image conversion GUI with Python programming language. Feel free to ask your valuable questions in the comments section below.

related posts

Leave a Comment