You may use the following technique to convert PNG to JPG using Python:
from PIL import Image im1 = Image.open(r'path where the PNG is stored\file name.png') im1.save(r'path where the JPG will be stored\new file name.jpg')
Next, I’ll review the steps to convert PNG to JPG using a simple example. I’ll also include the code to create the following tool to convert your files:
Steps to Convert PNG to JPG using Python
Step 1: Install the PIL package
The PIL package can be used to perform the conversion from PNG to JPG.
You can therefore install this package using the command below (under Windows):
pip install Pillow
You may wish to refer to the following guide for the steps to install a Python package under Windows.
Step 2: Capture the path where the PNG is stored
Next, capture the path where the PNG file is stored on your computer.
In my case, I stored a PNG file (called ‘summer’) under the following path: C:\Users\Ron\Desktop\Test
Step 3: Convert the PNG to JPG using Python
Finally, you may use the template below in order to convert your PNG to JPG:
from PIL import Image im1 = Image.open(r'path where the PNG is stored\file name.png') im1.save(r'path where the JPG will be stored\new file name.jpg')
For our example:
- The path where the PNG is currently stored is: C:\Users\Ron\Desktop\Test
- Where the file name is ‘summer’ and the file extension is ‘png’
- The path where the JPG will be stored is: C:\Users\Ron\Desktop\Test
- Where the new file name is ‘new_summer’ and the file extension is ‘jpg’
So this is the complete Python code to convert the PNG to JPG for our example (make sure to adjust the paths to reflect the location where the files will be stored on your computer):
from PIL import Image im1 = Image.open(r'C:\Users\Ron\Desktop\Test\summer.png') im1.save(r'C:\Users\Ron\Desktop\Test\new_summer.jpg')
Run the code (after adjusting your paths), and you’ll get the new JPG file at your specified location:
Tool to Convert PNG to JPG
You may use the tool below in order to convert your PNG to JPG. This tool is based on the tkinter package, which can be used to build a Graphical User Interface in Python.
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 = 'azure3', relief = 'raised') canvas1.pack() label1 = tk.Label(root, text='File Conversion Tool', 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) browseButton_PNG = tk.Button(text=" Import PNG File ", command=getPNG, bg='royalblue', fg='white', font=('helvetica', 12, 'bold')) 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, bg='royalblue', fg='white', font=('helvetica', 12, 'bold')) canvas1.create_window(150, 180, window=saveAsButton_JPG) root.mainloop()
Run the code in Python, and you’ll see the following display:
Click on the ‘Import PNG File‘ button:
Locate your PNG file that you’d like to convert, and then click on Open:
Next, click on the ‘Convert PNG to JPG‘ button to perform the conversion:
Type a name for your new JPG file, such as ‘new_summer‘ and then click on Save:
Your new JPG file will appear at your specified location:
At times, you may need to convert JPG to PNG. If that’s the case, you may want to check the following guide that explains the steps to convert JPG to PNG using Python.