The following syntax can be used to convert JPG to PNG using Python:
from PIL import Image im1 = Image.open(r'path where the JPG is stored\file name.jpg') im1.save(r'path where the PNG will be stored\new file name.png')
Next, I’ll show you how to apply the above syntax in practice. I’ll also include the code to create the following tool to convert your images:
Steps to Convert JPG to PNG using Python
Step 1: Install the PIL Package
If you haven’t already done so, install the PIL package using the command below:
pip install Pillow
You may check the following guide that explains how to install a package in Python under Windows.
Step 2: Capture the Path where the JPG is Stored
Next, capture the path where your JPG file is stored.
For example, I stored a JPG file (called ‘autumn’) under the following path:
C:\Users\Ron\Desktop\Test
Step 3: Convert the JPG to PNG using Python
Finally, you may use the syntax below in order to convert the JPG to PNG using Python:
from PIL import Image im1 = Image.open(r'path where the JPG is stored\file name.jpg') im1.save(r'path where the PNG will be stored\new file name.png')
For our example:
- The path where the JPG is currently stored is: C:\Users\Ron\Desktop\Test
- Where the file name is ‘autumn‘ and the file extension is ‘jpg‘
- The path where the PNG will be stored is: C:\Users\Ron\Desktop\Test
- Where the new file name is ‘new_autumn‘ and the file extension is ‘png‘
So this is how the complete Python code would look like for our example (you’ll need to modify 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\autumn.jpg') im1.save(r'C:\Users\Ron\Desktop\Test\new_autumn.png')
Run the code (adjusted to your paths), and the new PNG file will get created at your specified location:
Tool to Convert JPG to PNG
In the final section of this guide, I’ll share the code to create a simple tool to convert your JPG to PNG.
This tool is based on the tkinter package, which can be used to create 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 = 'lightsteelblue2', relief = 'raised') canvas1.pack() label1 = tk.Label(root, text='File Conversion Tool', bg = 'lightsteelblue2') label1.config(font=('helvetica', 20)) canvas1.create_window(150, 60, window=label1) def getJPG (): global im1 import_file_path = filedialog.askopenfilename() im1 = Image.open(import_file_path) browseButton_JPG = tk.Button(text=" Import JPG File ", command=getJPG, bg='royalblue', fg='white', font=('helvetica', 12, 'bold')) canvas1.create_window(150, 130, window=browseButton_JPG) def convertToPNG (): global im1 export_file_path = filedialog.asksaveasfilename(defaultextension='.png') im1.save(export_file_path) saveAsButton_PNG = tk.Button(text='Convert JPG to PNG', command=convertToPNG, bg='royalblue', fg='white', font=('helvetica', 12, 'bold')) canvas1.create_window(150, 180, window=saveAsButton_PNG) root.mainloop()
Run the above code in Python, and you’ll see this display:
Click on the ‘Import JPG File‘ button in order to import your JPG file that you’d like to convert to PNG:
Now locate your JPG file. In our case, locate the ‘autumn‘ file, and then click on Open:
Next, press on the ‘Convert JPG to PNG‘ button to complete the conversion:
Finally, type a new name for the PNG file to be created (for example, type ‘new_autumn‘), and then click on Save:
Your new PNG file will now get created at your specified location:
You may also want to check the following guide that explains the steps to convert PNG to JPG using Python.