DEV Community

Trilochan Parida
Trilochan Parida

Posted on

1 1

How to Convert PNG to JPG using Python

You need to install Pillow package to convert PNG to JPG.

pip3 install Pillow

Then run below code and your PNG to JPG Tool is ready.

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

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

label1 = tk.Label(root, text='PNG to JPG Tool', bg='gray')
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).convert('RGB')

browseButton_PNG = tk.Button(text="   Import PNG file  ", command=getPNG)
canvas1.create_window(150,130,window=browseButton_PNG)

def convertToJPG():
    global im1

    export_file_path = filedialog.asksaveasfilename(defaultextension='.jpg')
    im1.save(export_file_path)

saveAsButton_JPG = tk.Button(text='Convert PNG to JPG', command=convertToJPG)
canvas1.create_window(150, 180, window=saveAsButton_JPG)

root.mainloop()
Enter fullscreen mode Exit fullscreen mode

If any doubt, please comment below.

Watch the full course on Python: https://www.youtube.com/playlist?list=PLpzY0hUPKIeeRj3jOdC2YRD_MfCtBgDEe

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (2)

Collapse
 
awise777 profile image
awise777

You can also do it online: freetools.site/image-converters/pn...

Collapse
 
kirtan0000 profile image
Kirtan Magan

You are amazing!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more